Possible to do a one liner??? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

Possible to do a one liner???

POP SNAP CRACKLE SOLUTION one = int(input()) two = int(input()) three = int(input()) four = int(input()) five = int(input()) six = int(input()) list = [one, two, three, four, five, six] final = [] for i in list: if i%3 == 0: s = "Pop" final.append(s) elif i%2 == 1: s = "Snap" final.append(s) else: s = "Crackle" final.append(s) print(' '.join(final))

22nd Jun 2022, 10:50 AM
Evgeny Sergeev
Evgeny Sergeev - avatar
15 Antworten
+ 3
print(*[("Pop","Snap","Crackle")[(0,1,2,0,2,1)[int(input())%6]]for _ in"Krispy"]) https://code.sololearn.com/cBl54mZ6XjsX/?ref=app
25th Jun 2022, 7:41 AM
Brian
Brian - avatar
+ 2
Yes but you have got a code ugly as hell. I think is better optimize your code: final =''" For i in range(6): val = int(input()) If val%3 == 0: final = final + "Pop " Elif val%2 == 0: final = final + "Snap " Else: final = final + "Crackle " print(final.rstrip())
22nd Jun 2022, 11:29 AM
David Delgado
David Delgado - avatar
+ 2
Rstrip trims final withespaces
22nd Jun 2022, 11:46 AM
David Delgado
David Delgado - avatar
+ 2
Not a one-liner, but easy to understand 😊 in_list = [input(), input(), input(), input(), input(), input()] result = "" for x in in_list: if int(x) % 3 == 0: result += "Pop " elif int(x) % 3 != 0 and int(x) % 2 != 0: result += "Snap " else: result += "Crackle " print(result.rstrip())
27th Jun 2022, 5:59 PM
Martin Valdés Mallaug
+ 1
David Delgado my specialty lies in web-based coding, and it seems you are more experienced in data science than me, so no arguments here (-: But to pick a little on your code, I would have done "final += string" instead of "final = final + string" to type less :D But thanks for the feedback and your code, I adopted your way of taking the input() into the for loop - way better 😊👌 And also the .rstrip() instead of result[:-1]
27th Jun 2022, 6:45 PM
Martin Valdés Mallaug
0
David Delgado whats rstrip do? never encountered Thanks for feedback
22nd Jun 2022, 11:43 AM
Evgeny Sergeev
Evgeny Sergeev - avatar
0
✩✮★✮✩ crazy cool, if it works, Why whoudl crackle and snap be in the same position and with consitoon i%2 and how do we know the correct one will be printed ?
23rd Jun 2022, 9:57 AM
Evgeny Sergeev
Evgeny Sergeev - avatar
0
✩✮★✮✩ amazing WhAt _ standss for ?
23rd Jun 2022, 7:01 PM
Evgeny Sergeev
Evgeny Sergeev - avatar
0
In my opinion is better don't store variables if you don't need it for the output and save memory reusing the same. But it's not important in this case because is a little chunk of code.
27th Jun 2022, 6:12 PM
David Delgado
David Delgado - avatar
0
It's only a way to manage the assignment because of the original C way. I never remember if += works in a language if I'm not in the IDE and try it first ;P I prefer your way too.
27th Jun 2022, 6:48 PM
David Delgado
David Delgado - avatar
0
Yeah I get that, and I see that many great coders stick to it either way. I was just picky for the fun of it ;D Buenas tardes amigo (-:
27th Jun 2022, 6:53 PM
Martin Valdés Mallaug
0
# shorter than ✩✮★✮✩ print(' '.join(['Pop','Crackle','Snap'][i%3]for i in map(int,input().split())))
9th Jul 2022, 5:06 PM
Ervis Meta
Ervis Meta - avatar
0
Ervis Meta - SoloHelper good so far. Now fulfill the requirement to get six inputs.
9th Jul 2022, 6:29 PM
Brian
Brian - avatar
0
Just insert how many numbers you want separated by spaces and it'll work no matter that.
9th Jul 2022, 6:34 PM
Ervis Meta
Ervis Meta - avatar
0
Ervis Meta - SoloHelper yep, I like how you made that that work. Unfortunately, the Code Coach inputs come on separate lines. Edit: I tried it in Code Coach and found that it didn't output the right sequence for Test Case 2: 100 200 300 150 250 350.
9th Jul 2022, 6:47 PM
Brian
Brian - avatar