Discuss Scratch

bybb
Scratcher
1000+ posts

Code Golf.

Couldn't find one on the Google on the ATs so I decided to make one (please tell me if it's a dupe)
The idea is simple, someone say a program / algorithm to golf, and everyone will try and golf it!

Some starters:
FizzBuzz
Palindrome
Anagram

Some I've done (Python):
FizzBuzz:
print("".join(((("Fizz"if i%3<1 else"")+("Buzz"if i%5<1 else""))or str(i))+"\n" for i in range(1, 101)))
Palindrome:
def palindrome(x):
    return(lambda x:x==x[::-1])([i.lower()for i in x if i.isalnum()])

Use any language you wish.

Game Over
You'll find me on @LastContinue from now on.
red_king_cyclops
Scratcher
500+ posts

Code Golf.

One small question: Did my topic inspire you to make this topic?

2+2=4
2*2=4
2^2=4
2^^2=4
2^^^2=4
2^^^^2=4

I see a pattern.
ninjagolloyd
Scratcher
500+ posts

Code Golf.

never golf in a language that requires whitespace

bean
bybb
Scratcher
1000+ posts

Code Golf.

ninjagolloyd wrote:

never golf in a language that requires whitespace
I like the challenge of removing as much whitespace as possible by changing the code in different ways

Game Over
You'll find me on @LastContinue from now on.
ninjagolloyd
Scratcher
500+ posts

Code Golf.

bybb wrote:

ninjagolloyd wrote:

never golf in a language that requires whitespace
I like the challenge of removing as much whitespace as possible by changing the code in different ways
but whitespace is still required

bean
jokebookservice1
Scratcher
1000+ posts

Code Golf.

ninjagolloyd wrote:

bybb wrote:

ninjagolloyd wrote:

never golf in a language that requires whitespace
I like the challenge of removing as much whitespace as possible by changing the code in different ways
but whitespace is still required
I take issue with all characters when I'm golfing. I despise the ‘k’ character, for example, because it takes up a whole byte. Yet I still golf in such languages, because all languages are made up of characters, which all take up entire bytes, and I lack a choice. I, personally, don't find the white-space character special in this regard.

If you just want short outputs, you might wish to use languages specifically designed for code golf. But generally, people compare code golf between identical languages, so that it's fair if you're using ‘normal’ languages – and so length shouldn't be a concern, therefore, when selecting your language.

If you find it difficult to detect the white space character visually, use a font where it doesn't look like empty space.

The byte associated with 32 doesn't have to map to a whitespace character in your encoding system either (this is quite similar to changing your font). However, note that choosing to never use the 32-nd byte means that you're able to write less programs in the same number of characters; which is bad for golfing
ninjagolloyd
Scratcher
500+ posts

Code Golf.

jokebookservice1 wrote:

ninjagolloyd wrote:

bybb wrote:

ninjagolloyd wrote:

never golf in a language that requires whitespace
I like the challenge of removing as much whitespace as possible by changing the code in different ways
but whitespace is still required
I take issue with all characters when I'm golfing. I despise the ‘k’ character, for example, because it takes up a whole byte. Yet I still golf in such languages, because all languages are made up of characters, which all take up entire bytes, and I lack a choice. I, personally, don't find the white-space character special in this regard.

If you just want short outputs, you might wish to use languages specifically designed for code golf. But generally, people compare code golf between identical languages, so that it's fair if you're using ‘normal’ languages – and so length shouldn't be a concern, therefore, when selecting your language.

If you find it difficult to detect the white space character visually, use a font where it doesn't look like empty space.

The byte associated with 32 doesn't have to map to a whitespace character in your encoding system either (this is quite similar to changing your font). However, note that choosing to never use the 32-nd byte means that you're able to write less programs in the same number of characters; which is bad for golfing
i just dont like whitespace it doesnt make sense and its too easy to mess up

bean
CatIsFluffy
Scratcher
100+ posts

Code Golf.

Then why do you think we shouldn't use languages that require whitespace?
Sheep_maker
Scratcher
1000+ posts

Code Golf.

palindrome=x=>(t=>t+''==t.reverse())(x.toLowerCase().replace(/\W/g,'').split(''))
palindrome in JavaScript (ignores characters that aren't a \w in regex, which apparently includes _ and accented characters)

anagram=(...a)=>([a,b]=a.map(v=>v.split('').sort()),a+''==b)
anagram in JavaScript

- Sheep_maker This is a kumquat-free signature. :P
This is my signature. It appears below all my posts. Discuss it on my profile, not the forums. Here's how to make your own.
.postsignature { overflow: auto; } .scratchblocks { overflow-x: auto; overflow-y: hidden; }
1a3c5e7g9i
Scratcher
100+ posts

Code Golf.

Random number guessing game in Python 3, defined here: https://adriann.github.io/programming_problems#elementary
(My new method saves 17 bytes)
import random
n=random.randint(1,5)
t=0
g=0
while g!=n:
    g=int(input())
    print(1 if g<n else 2 if g>n else'')
    t+=1
print(t)

Last edited by 1a3c5e7g9i (Nov. 4, 2018 03:35:20)



Wao how is that possible





Uh Squidward? Yes Spongebob? You might want to look at this… HOLY SHRIMP! If we don't act now, well, think about how many clams I'll have to cough up JUST to see an extra portion of nick.com… or worse, to see ALL of it in general! *** Reader! This is not a fictional sketch! It actually is, but that's not the point. Learn more at the link that Spongebob left at the first instance of “this”.
ScratchMan544
Scratcher
100+ posts

Code Golf.

1a3c5e7g9i wrote:

Random number guessing game in Python 3, defined here: https://adriann.github.io/programming_problems.html
(My new method saves 17 bytes)
import random
n=random.randint(1,5)
t=0
g=0
while g!=n:
    g=int(input())
    print(1 if g<n else 2 if g>n else'')
    t+=1
print(t)

You can save another 38 characters by more questionable means:

from random import*
i=g=0
a=randint(1,5)
while i-a:i=int(input());g+=1;print(((a>i)-(a<i))or g)

Last edited by ScratchMan544 (Oct. 13, 2018 11:54:44)


_=(lambda _:lambda __:_(__))(lambda _:getattr(_,(
    lambda _:_[:2]+str(print.__call__)[0b10011:(1+1<<1+1+1)+(1<<1+1)+(1<<1)+1]+_[-2:]
)(__name__)))(eval)
(lambda _:lambda __:_(__))(lambda _:_(_(
    __import__(dir(__builtins__)[((1<<1+1)<<1+1+1)+(1+1<<1+1+1)+(1+1<<1)+(1<<1)][:3].lower()),
    print.__doc__[46:52]),open(__file__).write.__str__()[17:22]))(_("getattr"))((
    lambda _:lambda __:_(_,__))(lambda _,__:""if __==0else chr(__%128)+_(_,__//128))(963149002634454890336513358634316810781103160855182366005237514)[::-1]
)
red_king_cyclops
Scratcher
500+ posts

Code Golf.

bump

2+2=4
2*2=4
2^2=4
2^^2=4
2^^^2=4
2^^^^2=4

I see a pattern.
MegaApuTurkUltra
Scratcher
1000+ posts

Code Golf.

fizzbuzz in C

#define p printf
main(i){i%3?i%5?p("%d\n",i):p("buzz\n"):i%5?p("fizz\n"):p("fizzbuzz\n");i<100&&main(++i);}

don't call it with any arguments

Last edited by MegaApuTurkUltra (Oct. 31, 2018 23:49:52)


$(".box-head")[0].textContent = "committing AT crimes since $whenever"
red_king_cyclops
Scratcher
500+ posts

Code Golf.

Shortest Hello World program (in Bash):
echo Hello world
or
echo Hello, world!

2+2=4
2*2=4
2^2=4
2^^2=4
2^^^2=4
2^^^^2=4

I see a pattern.
MegaApuTurkUltra
Scratcher
1000+ posts

Code Golf.

red_king_cyclops wrote:

Shortest Hello World program (in Bash):
echo Hello world
or
echo Hello, world!
Hello world in HQ9+

H

$(".box-head")[0].textContent = "committing AT crimes since $whenever"
ThetaHacker
New to Scratch
9 posts

Code Golf.

bybb wrote:

Couldn't find one on the Google on the ATs so I decided to make one (please tell me if it's a dupe)
The idea is simple, someone say a program / algorithm to golf, and everyone will try and golf it!

Some starters:
FizzBuzz
Palindrome
Anagram

Some I've done (Python):
FizzBuzz:
print("".join(((("Fizz"if i%3<1 else"")+("Buzz"if i%5<1 else""))or str(i))+"\n" for i in range(1, 101)))
Palindrome:
def palindrome(x):
    return(lambda x:x==x[::-1])([i.lower()for i in x if i.isalnum()])

Use any language you wish.

BEFORE:
print(“”.join((((“Fizz”if i%3<1 else“”)+(“Buzz”if i%5<1 else“”))or str(i))+“\n” for i in range(1, 101)))

AFTER:
print(“\n”.join((((“Fizz”if i%3<1 else“”)+(“Buzz”if i%5<1 else“”))or str(i))for i in range(1,101)))

^ 5 letter compression (two quotes, two spaces, one plus)

(NOTE: When putting code in replace the quotes “” with the standard python quotes or it will give an error from the different unicode upload used in scratch compared to pythons)

Last edited by ThetaHacker (Nov. 4, 2018 06:25:43)

CatIsFluffy
Scratcher
100+ posts

Code Golf.

Use [code][/code] tags to avoid formatting issues
print("\n".join(((("Fizz"if i%3<1 else"")+("Buzz"if i%5<1 else""))or str(i))for i in range(1,101)))
1a3c5e7g9i
Scratcher
100+ posts

Code Golf.

Late post, but…
Code that returns a True/False list for each item in an iterable (e.g. a list).
e=lambda l:[bool(i)for i in l]
Replacing the brackets with parentheses returns a generator you can get values from.
Using braces returns a set. A potential replacement for all(), since sets only have 1 unique instance of each item.

Last edited by 1a3c5e7g9i (Feb. 8, 2019 07:48:33)



Wao how is that possible





Uh Squidward? Yes Spongebob? You might want to look at this… HOLY SHRIMP! If we don't act now, well, think about how many clams I'll have to cough up JUST to see an extra portion of nick.com… or worse, to see ALL of it in general! *** Reader! This is not a fictional sketch! It actually is, but that's not the point. Learn more at the link that Spongebob left at the first instance of “this”.
liamdadude
Scratcher
500+ posts

Code Golf.

What's golf?


Hi! Im liamdadude! check out my website
NitroCipher
Scratcher
500+ posts

Code Golf.

liamdadude wrote:

What's golf?

Code golf is the concept of trying to complete a task, with as little characters in the code as possible; Kinda like how lower scores are better in golf!

I hope my post helped you in some way! Post count: 500+

Current project: [s3Blocks: scratchblocks rewritten for Scratch 3.0] ::#4b4a60 //https://scratch.mit.edu/discuss/topic/290031/ Basically done!
This is my signature identifier “aWFtbml0cm9jaXBoZXI=”

Powered by DjangoBB