As long as you enter a string that is a valid integer, ie „42“,
If you would enter a string that is a float ie „42.1“ You would lose the devimal places -> 42
It is weird as the input would always be a single int at most, so they could replace the map and sum ( i guess they are summing as the map would produce a generator) to a single int statement.
This whole thing could be reduced to int(input(„Enter number „)).
It would still fail if the input string wiuld be domething other tha a valid int or float, but at least it does not take the long way round.
You can remove the [] to make it a generator expression, which makes this more efficient. Instead of having to turn the entire thing into a list, which then gets iterated by sum again, just have each element be processed one by one. Or, you know, sum(map(int, input()))…
Very clever. Looks like it works as intended, and this may well be considered the "pythonic" way to solve this problem - although as noted below, list comprehensions are more readable and clear.
Anyhow, I'm writing this to say that "clever" is often a problem in software. I don't think that this is bad code - it works, and someone who understands (or is willing to look up) the calls can see how it works. But neat and clever tricks are fragile, and fragility is a direct path to agony in professional software development.
It should. Input will treat the input number as strings and tuple will split the input, map runs them through int(), and sum will then sum up the values in the tuple. So the sum of each digit of the input number
Something like this (untested and from iPhone, so I'd expect a typo or 2) would allow the code you've proposed. I wouldn't do this for builtins like here but it's a neat way of representing pipelines in general.
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/gabrielfv, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
It does not split it by spaces. A string is an iterable, and iterating over it yields a sequence of its individual characters. The tuple constructor takes any iterable and turns it into a tuple of its individual elements. But so does map, it also takes any iterable. So you can omit the step of turning a string iterable into a tuple iterable, and just directly map over the string.
Really? Hmm... Then I'm confused. If that's right, I agree; that should be eliminated. That makes me question why they would want to split their input number and sum the digits in the first place.
Honestly, I don't think it's that bad. The only parts that really bother me are the tuple call and the punctuation.
Other comments say the call to tuple splits the input string by spaces. If that's right, that's clever, but not intuitive to read. But, I'm not that deep into Python; maybe for people with more Python experience, this is common.
In terms of punctuation, a space after the "int," could actually make a big difference. Other than that, I'd love to reduce those parentheses, but I don't know if there's much to be done there in Python.
It's worth noting that I like writing in Haskell, thus the aversion to parentheses and the affinity for this function-chaining style of coding, but many people aren't used to functional and probably won't read that too well. It's something to keep in mind, but I don't think that automatically makes it bad.
Update: this comment says tuple doesn't work that way after all, which means the tuple call probably shouldn't exist at all. If it truly is superfluous, removing it would definitely be a significant improvement.
17
u/AmoghMadan31 Aug 19 '21
Does that even work???
10
u/deceze Aug 19 '21
The
tuple()is perfectly superfluous.83
u/retrolasered Aug 19 '21
Nah, that's self commenting
77
u/Knuffya Aug 19 '21
33
u/BinaryBlasphemy Aug 19 '21
Lol this is just functional programming
23
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Aug 19 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
10
u/REALDrummer Aug 19 '21
Honestly, I don't think it's that bad. The only parts that really bother me are the tuple call and the punctuation.
Other comments say the call to tuple splits the input string by spaces. If that's right, that's clever, but not intuitive to read. But, I'm not that deep into Python; maybe for people with more Python experience, this is common.
In terms of punctuation, a space after the "int," could actually make a big difference. Other than that, I'd love to reduce those parentheses, but I don't know if there's much to be done there in Python.
It's worth noting that I like writing in Haskell, thus the aversion to parentheses and the affinity for this function-chaining style of coding, but many people aren't used to functional and probably won't read that too well. It's something to keep in mind, but I don't think that automatically makes it bad.
1
u/Emanuel62 Aug 19 '21
How did u get access to my laptop?
1
u/StaryMD Aug 19 '21
yeah, at the beginning if the line
1
u/Tankznor Aug 20 '21
Print sum map pls
1
u/rajesh_dude0 Aug 20 '21
When they ask you to strictly follow functional programming.
1
u/sktr-guys Aug 20 '21
callback hell