Timeline for Can this code, to convert string to integer, be made more compact?
Current License: CC BY-SA 4.0
13 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Aug 20, 2019 at 12:54 | history | edited | eric.m | CC BY-SA 4.0 |
Alternative to isdigit() and fixed range calculation.
|
Aug 9, 2019 at 15:30 | vote | accept | tarit goswami | ||
Aug 8, 2019 at 6:35 | history | edited | eric.m | CC BY-SA 4.0 |
edited body
|
Aug 7, 2019 at 17:10 | comment | added | Roberto |
Would you also suggest a variable other than "l", given how similar it looks to "1" (if i == l , etc)?
|
|
Aug 7, 2019 at 16:16 | comment | added | Graipher |
@taritgoswami No, it is a unicode symbol, a 1 that is raised, to be used as an exponent,. It still counts as a digit to str,isdigit , but int("¹") fails with a ValueError . So using str.isdigit is not sufficient to reject values that are not parseable by int . Your only options are explicitly whitelisting only the ASCII digits (which is what string.digits contains), or by trying to parse a digit with int and skipping it if an exception is raised.
|
|
Aug 7, 2019 at 16:10 | comment | added | tarit goswami |
@Graipher Thanks for mentioning. Is it "1".isdigit() ?
|
|
Aug 7, 2019 at 14:01 | comment | added | Graipher |
Beware, though, even isdigit has its flaws. "¹".isdigit() returns True . The only way to be really sure I know of is to test x in set(string.digits) or similar.
|
|
Aug 7, 2019 at 13:59 | comment | added | tarit goswami | Thanks for your time :) Can I get any suggestion about analyzing and noting down all requirements for this kind of problems? | |
Aug 7, 2019 at 9:15 | comment | added | Toby Speight | Oh, absolutely - I was just having a little fun with the words! | |
Aug 7, 2019 at 9:12 | history | edited | eric.m | CC BY-SA 4.0 |
added 101 characters in body
|
Aug 7, 2019 at 9:10 | comment | added | eric.m | I thought he meant compact in less lines or less code. I cannot see any benefit of removing whitespaces when they make the code more readable. | |
Aug 7, 2019 at 8:54 | comment | added | Toby Speight | If the requirement really is to make the code "more compact", then perhaps it's reasonable to throw PEP-8 out of the window. But it does make us ask, "why is that a goal??".... | |
Aug 7, 2019 at 8:36 | history | answered | eric.m | CC BY-SA 4.0 |