Hopscotch Programming Puzzles


#1

Hey everyone! The constraints of Hopscotch make for some really interesting challenges. Use that!

Background for idea

I came across Programming Puzzles and Code Golf StackExchange site the other day. And as I was solving a problem that had caught my eye, I remember the thrill of the Challenge Math and Brain Teaser Problems topic in the maths category here. So I thought, why don’t we have a programming puzzle topic here?

Post puzzles and answers here — anyone can join.

And post your thought processes! Remember to hide them in a details or a spoiler tag though, to let others come across their own solutions too.

and yep, it doesn’t have to be a complete answer. Trying is just as important! Elaborating on your thought processes so far may even bring you to some more ideas to try out. Post screenshots of your code, and talk about what you were thinking through.)


#2

Here is one that I thought was interesting to do in Hopscotch :smile:

Fibonacci sequence

The Fibonacci sequence is a series of numbers where the next number equals the sum of the two preceding terms.

0, 1, 1, 2, 3, 5, 8, 13, 21, …

Write a Hopscotch program that determines the n-th term of the Fibonacci sequence, where the 1st term is 0 and the 2nd term is 1.

You should be able to specify n in your program, and the n-th term of the Fibonacci sequence will be the final result.


#3

Awesome coding problems! I love it!

I unpublished it since I haven't completed the full idea, but here is the Fibonacci Sequence:
https://c.gethopscotch.com/p/yu11rh5cq
(Yeah, I'm going to make an approximation bar for really large numbers! :D)


#4

Cool! I will work on that right now!


#5

Here is mine. You just put in the term you want it to calculate and it does. Although if the term is to large it will just display "inf".

My Project

My Project Link


#6

This is a cool topic! I will try to do it, it is possible but if you code it in Python (or any other coding language) you can create lists inside variables. However, I don't use the new editor to code, it doesn't work. So, I haven't tried Self and all of the other new features, which means that it could work. I would like to try it, but the newer update won't load my drafts at all...


#7

It is not much, I wish I could have added more.


#8

This is an interesting topic :smiley:


#9

Great topic idea! I think I am going to be hanging out here quite a bit :)


Here is a very simple program in Hopscotch that calculates the n-th term of the Fibonacci sequence


#10

Woah these are wonderful!

Wow, I thought that was great already (I think it looks awesome as well) and I see you are thinking about adding functionality for calculating an approximation for really big numbers :scream: that's amazing.

This is really cool! I love how you added the ability to choose a number from playing the project as well, I really didn't expect that. (For my ones, I just let there be a variable in the code that I could change :joy: so that was the basic minimum I set in the requirement of 'being able to specify the input n')

Yeah it is really handy what you can do in other languages!

And oh no that must be pretty frustrating :cry: (I would like to add, this doesn't have to be in the newest updates :smiley: this idea was actually inspired by when Hopscotch didn't have a lot of functionalities like a long time ago!)

For MR.GAM3R

Very neat! I think that's really cool how you did it with just two variables current and previous. I remember when I first did it, I used a, b, c. That was totally unreadable XD so I just went back and changed it now:

(The initialisation here for nth fib number is a little dodgy...) I like how succinct yours is!

Edit: oh wait, in my picture, I think that should be repeat times(n-1), not n, according to the two starting numbers I defined in the problem.

And yep haha I'm really glad hearing you're interested in hanging around here (I love coming across topics here that make me feel like that :heart:)

And this is one idea I was thinking through (aside from the code I put in the previous details):

Here is something I was thinking through recently

I was interested in doing this with recursion in Hopscotch (even though it is an inefficient way of approaching Fibonacci's without a dictionary data type :slight_smile:)

This is the pseudocode idea (thinking but not actually coding anything yet)

fibonacci(n):
    if n = 1 then
        return 0
    else if n = 2 then
        return 1
    else
        return (fibonacci(n-2) + fibonacci(n-1))

In Hopscotch, there are a few things you have to think about with recursion

This was the code I had so far:

The problem is, the variables (n-2)th fib number and (n-1)th fib number don't record as local variables for each time (each time the Fibonacci ability is run, they're both reset) :thinking:

So I'm stuck there for now.


And again, this is just like the Challenges topic – anyone can submit a puzzle, and anyone can solve whichever they feel like :blush: (also inspiration from various other programming competitions)

Oh yeah, I think I forgot to mention, but the heart of this was also inspired by when Hopscotch first announced that it was Turing Complete on its blog – meaning that it can technically be used to solve any computer problem in the world. This made me go like woah :open_mouth: so I was really inspired to try solving problems using just Hopscotch, leading to these ideas initially starting :upside_down:


#11

I have another challenge but I will have to post it later on. Or actually, this is basically it:

Remainder after a division

Without using the modulo function!

Write a Hopscotch program that calculates the remainder after a division. You can use everything except for the modulo function.

Here are some basic ideas on remainders in the modulo topic if you haven't heard of them before :smile:

Example cases:

25 divided by 2 is remainder 1
4 divided 9 is remainder 4
55 divided by 10 is remainder 5

Inputs:
Someone looking at your program should be able to specify the number to divide into (the dividend), and the number to divide by (the divisor).

Outputs:
The program should output the remainder when the dividend is divided by the divisor. (By the way, the result is called the quotient. And yep I just looked these up to check :thumbsup:)

I'm really interested in seeing your Hopscotch code and hearing about your thought processes, so feel very welcome to share those!


#12

I have never heard of modulo so this should be no problem for me not to use!


#13
These were some thoughts I had, even though I've done this before actually :)

The first time I did it, I ran on the idea that division is repeated subtraction. You keep subtracting the divisor from the number until the result is smaller than the divisor.

set number to dividend
set divisor
repeat until number < divisor
    set number to number - divisor
end
set remainder to number
set text(remainder)

I have another idea using fractions. e.g. if you do 51 / 9, you get 5 and 6/9. If you multiply the fractional part by the divisor, 9, you will get the remainder.

set quotient to dividend/divisor
set floor_quotient to round(quotient - 0.5)
set fractional_remainder to (quotient - floor(quotient))
set remainder to (fractional_remainder * divisor)

I haven't gotten to test this yet though.


#14

I was planning on doing the second one. The first one sounds much easier.


#15

I am trying to make a prime number calculator. There is one problem. I don't know how to set up the code... Could you all help me?


#16

Cool idea! I made one in JavaScript, here is some of the code:

var number = 11
//For boolean values (true or false), you can use 0 & 1.
var prime = true

// this is basically Repeat number times
for (i = 0; i < number; i++) {

  If number % i = 0 {
     prime = false
  }
 
}

If prime {
// this is a prime number
} else {
// this is not a prime number 
}


#17

I'm going to reword this one :slight_smile: This is all, basically:


Remainder after a division

Without using the modulo function!

Write a Hopscotch program that calculates the remainder after a division. You can use everything except for the modulo function.


#18

Do we need the quotient also? Or just the remainder? Or both?


#19

You can just focus on the remainder :relaxed: if you would like to put quotient, you can also do that :smiley:


#20

Okay, I will try to do both.