Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » What are some programming problems like "Hello, world" and "FizzBuzz"?
- red_king_cyclops
-
Scratcher
500+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
I've seen “Hello, world” programs in many languages, and I've see “FizzBuzz” programs in several languages here. What are some more programming problems like “Hello, world” and “FizzBuzz”?
Edit 9/6/2018: You may post code that answers a problem posted here.
Edit 9/6/2018: You may post code that answers a problem posted here.
Last edited by red_king_cyclops (Sept. 6, 2018 22:07:25)
- _nix
-
Scratcher
1000+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
A “cat” program, where the input is printed to the output, is pretty common.
More novel is the “quine”, where your program actually outputs its own code. I don't think you could really do that with Scratch, except maybe if you had a script like this
More novel is the “quine”, where your program actually outputs its own code. I don't think you could really do that with Scratch, except maybe if you had a script like this

when green flag clicked
switch costume to [screenshot of this script v]
show
- novice27b
-
Scratcher
1000+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
A “cat” program, where the input is printed to the output, is pretty common.https://scratch.mit.edu/projects/118091116/
More novel is the “quine”, where your program actually outputs its own code. I don't think you could really do that with Scratch, except maybe if you had a script like thiswhen green flag clicked
switch costume to [screenshot of this script v]
show
- herohamp
-
Scratcher
1000+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
I have also heard of “Anagram” which is just write a program that returns true if you input two strings and they are an anagram.
- badatprogrammingibe
-
Scratcher
500+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
A quine is one.
Last edited by badatprogrammingibe (Oct. 2, 2018 11:46:13)
- Za-Chary
-
Scratcher
1000+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
A common one I've seen so far (which is also really annoying) is a program where you input a dollar amount in numerical form, and it prints out the dollar amount in longhand form, usually with a constraint that acceptable values are 0.00 to 1000000.00.
For example, if you were to type in:
then the program would output:
I've programmed this in both Python and Java, and I hated it because they were incredibly tedious to program. Not difficult, per se, but very tedious, since you need to make sure it doesn't print out stuff like
For example, if you were to type in:
- $1328.50
then the program would output:
- one thousand three hundred twenty eight and 50/100 dollars
I've programmed this in both Python and Java, and I hated it because they were incredibly tedious to program. Not difficult, per se, but very tedious, since you need to make sure it doesn't print out stuff like
- one thousand zero hundred ten five and 9/100 dollars
- jokebookservice1
-
Scratcher
1000+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
Za-Chary, did you use recursion in your solutions (this would probably be a good candidate for doing so).
This is some messy code, but pay attention to the “say” function.
It pronounces it the way I would (with an “and” between hundreds and tens).
Of course, I agree that there have to be special cases for stuff below 20, English is just plain weird, but with recursion everything looks very nice in my opinion – and could easily be extended to work beyond the billions.
This is some messy code, but pay attention to the “say” function.
MULTS_OF_TEN = ["zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"] BELOW_TWENTY = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "forteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"] # Join with spaces, but ignore empty elements def join(a): return " ".join([x for x in a if x != ""]) # Splits n into two chunks and adds strings to each side def _(n, div, postfix, conditionalPrefix = ""): return join( [say(n // div), postfix] + ([conditionalPrefix, say(n % div)] if n % div else []) ) # Take a look at how pretty this function is def say(n): if n >= 1000: return _(n, 1000, "thousand") if n >= 100: return _(n, 100, "hundred", "and") if n >= 20: return MULTS_OF_TEN[n // 10] + (" " + say(n % 10) if n % 10 else "") return BELOW_TWENTY[n] # numbers less than 20 def dollarsToString(string): [dollars, pennies] = map(int, string[1:].split(".")) return say(dollars) + " and " + str(pennies) + "/100 dollars"
It pronounces it the way I would (with an “and” between hundreds and tens).
print(dollarsToString("$123456.78")) # one hundred and twenty three thousand four hundred and fifty six and 78/100 dollars
Of course, I agree that there have to be special cases for stuff below 20, English is just plain weird, but with recursion everything looks very nice in my opinion – and could easily be extended to work beyond the billions.
- Za-Chary
-
Scratcher
1000+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
Za-Chary, did you use recursion in your solutions (this would probably be a good candidate for doing so).
I did not use recursion at all. I typed out iterative solutions with plenty of if-statements and switch-statements. Perhaps recursion could be a lot nicer, but I'd actually argue that an iterative solution would be easier, maybe? Especially with those special cases below twenty (which is necessary to fix for the sake of the problem).
Keep in mind that recursion, if used too much, may cause an error, but you probably won't use numbers large enough for that to happen.
I didn't look over your code too thoroughly, so I'm assuming it works. If it really does, that's a really nice solution, since it looks so concise!
- jokebookservice1
-
Scratcher
1000+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
These are the sorts of steps it takes, and I'd argue recursion is acceptable in this case. The special cases are handled by hard-coding them – and I'd argue this is acceptable – and if not, they can be generated (iteriatively) if needed.
For example, $123456 is “one hundred and twenty three thousand four hundred and fifty six” But notice that that's the same as
pronounce($123) + “thousand” + pronounce($456)
and then pronounce($123) is just
pronounce($1) + “hundred and” + pronounce($23)
and then pronounce($23) is just
pronounce($20) + pronounce($3)
I haven't had a go at the iterative solution so I suppose I can't argue which is easier to implement.
For example, $123456 is “one hundred and twenty three thousand four hundred and fifty six” But notice that that's the same as
pronounce($123) + “thousand” + pronounce($456)
and then pronounce($123) is just
pronounce($1) + “hundred and” + pronounce($23)
and then pronounce($23) is just
pronounce($20) + pronounce($3)
I haven't had a go at the iterative solution so I suppose I can't argue which is easier to implement.
- Za-Chary
-
Scratcher
1000+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
That makes sense, I suppose. Of course, I don't know how easy it is to program recursion for this particular problem. But even knowing all that I do, if I were to look at the problem for the first time, I'd probably try to write an iterative solution (even though that may end up being more work).
- bybb
-
Scratcher
1000+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
Anagram:
str1 = input("String 1:").capitalize() str2 = input("String 2:").capitalize() str1 = str1.replace(" ","") str2 = str2.replace(" ","") if len(str1) == len(str2): letters = {} for i in str1: if not i in letters.keys(): letters[i] = 1 else: letters[i] += 1 letters2 = {} for i in str2: if not i in letters2.keys(): letters2[i] = 1 else: letters2[i] += 1 if letters == letters2: print("Anagram") else: print("Not") else: print("Not")
- red_king_cyclops
-
Scratcher
500+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
Anagram:Great Python code.str1 = input("String 1:").capitalize() str2 = input("String 2:").capitalize() str1 = str1.replace(" ","") str2 = str2.replace(" ","") if len(str1) == len(str2): letters = {} for i in str1: if not i in letters.keys(): letters[i] = 1 else: letters[i] += 1 letters2 = {} for i in str2: if not i in letters2.keys(): letters2[i] = 1 else: letters2[i] += 1 if letters == letters2: print("Anagram") else: print("Not") else: print("Not")
Here's another one in Perl 5:
print "Type in two strings:\n"; print "String 1: "; my $input1 = <>; print "String 2: "; my $input2 = <>; my $string1 = $input1; my $string2 = $input2; $input1 =~ s/ //g; $input2 =~ s/ //g; my @chars1 = split//, $input1; my @chars2 = split//, $input2; @chars1 = sort @chars1; @chars2 = sort @chars2; if(lc(@chars1)==lc(@chars2)) { print "\"$string1\" is an anagram of \"$string2\", and vice versa.\n"; } else { print "\"$string1\" is not an anagram of \"$string2\", and vice versa.\n"; }
Last edited by red_king_cyclops (Aug. 23, 2018 16:04:06)
- onlyNones
-
Scratcher
61 posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
Fibonacci sequence.
The Fibonacci sequence is defined as (1,1,2,3,5,8,…)
Output:
Truth machine.
Given input as 0 or 1:
The Fibonacci sequence is defined as (1,1,2,3,5,8,…)
Output:
- Either the first terms of the Fibonacci sequence or
- The n-th term of the sequence.
Truth machine.
Given input as 0 or 1:
- If the input is 0, print 0 and terminate.
- If the input is 1, print 1 indefinitely.
Last edited by onlyNones (Sept. 2, 2018 02:35:53)
- goldfish678
-
Scratcher
1000+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
Fibonacci sequence.done.
The Fibonacci sequence is defined as (1,1,2,3,5,8,…)
Output:
- Either the first terms of the Fibonacci sequence or
- The n-th term of the sequence.
https://scratch.mit.edu/projects/244133721/
- Za-Chary
-
Scratcher
1000+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
Fibonacci sequence.
The Fibonacci sequence is defined as (1,1,2,3,5,8,…)
Output:You decide either to start at (0,1) or (1,1).
- Either the first terms of the Fibonacci sequence or
- The n-th term of the sequence.
Nice. The Fibonacci problem is a great problem because there are multiple solutions that take wildly different approaches.
There are iterative solutions, and recursive solutions, with and without memoization.
- onlyNones
-
Scratcher
61 posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
Primality test.
Given a number, output true if it's prime, and false if not.
Factorial.
Given a number n, output the factorial of n (n!).
The definition of a factorial (!) is (1*2*…*n)
If n is 0: 1.
First numbers:
Given a number, output true if it's prime, and false if not.
5 = true
2 = true
8 = false
99 = false
Factorial.
Given a number n, output the factorial of n (n!).
The definition of a factorial (!) is (1*2*…*n)
If n is 0: 1.
First numbers:
0 = 1
1 = 1
2 = 2
3 = 6
4 = 24
5 = 120
...
- bybb
-
Scratcher
1000+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
Primality test.
Given a number, output true if it's prime, and false if not.5 = true
2 = true
8 = false
99 = false
Factorial.
Given a number n, output the factorial of n (n!).
The definition of a factorial (!) is (1*2*…*n)
If n is 0: 1.
First numbers:0 = 1
1 = 1
2 = 2
3 = 6
4 = 24
5 = 120
...
def f(x): if x==0: return 1 else: return x*f(x-1)
- red_king_cyclops
-
Scratcher
500+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
Primality test.
Given a number, output true if it's prime, and false if not.5 = true
2 = true
8 = false
99 = false
Factorial.
Given a number n, output the factorial of n (n!).
The definition of a factorial (!) is (1*2*…*n)
If n is 0: 1.
First numbers:0 = 1
1 = 1
2 = 2
3 = 6
4 = 24
5 = 120
...def f(x): if x==0: return 1 else: return x*f(x-1)
Primality test in C:
#include <stdio.h> #include <stdbool.h> _Bool primetest(unsigned int x) { _Bool disprove = 0; for(int ctr = 2; ctr < x; ctr++) { if(x % ctr == 0) { disprove = 1; break; } } if(disprove) { return 0; } else { return 1; } } //Example code below int main(void) { printf("Enter a positive integer: "); unsigned int answer; scanf(" %u", &answer); printf("The number you entered is a %s.\n", ( primetest(answer) ) ? ("prime") : ("composite") ); }
- bybb
-
Scratcher
1000+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
Primality test in C:That's a bit complicated isn't it?#include <stdio.h> #include <stdbool.h> _Bool primetest(unsigned int x) { _Bool disprove = 0; for(int ctr = 2; ctr < x; ctr++) { if(x % ctr == 0) { disprove = 1; break; } } if(disprove) { return 0; } else { return 1; } } //Example code below int main(void) { printf("Enter a positive integer: "); unsigned int answer; scanf(" %u", &answer); printf("The number you entered is a %s.\n", ( primetest(answer) ) ? ("prime") : ("composite") ); }
def prime(x): print("Composite"if len("".join("1"for i in range(2,x)if x%i<1))else"Prime")
Last edited by bybb (Sept. 4, 2018 16:11:59)
- goldfish678
-
Scratcher
1000+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
scratch primality test in fifteen blocks (i starts at one)

edit: removed “stop this script” at very end and saved a block

edit: removed “stop this script” at very end and saved a block
Last edited by goldfish678 (Sept. 6, 2018 21:28:26)
- Discussion Forums
- » Advanced Topics
-
» What are some programming problems like "Hello, world" and "FizzBuzz"?