Re:Ask HN: starting a career in software dev in my thirties,...
by BioGeek on Thursday October 21, 2010 @03:13PM
(#1817011)
My Python answer for the second question, where `post` is patrio11's post as input:
from collections import defaultdict
from string import ascii_letters
d= collections.defaultdict(int)
for letter in post:
d[letter] += 1
print [letter for letter in sorted(d, key=d.get, reverse=True) if letter in ascii_letters][2]
Any comments on improvement for readability or conciseness?
Re:Ask HN: starting a career in software dev in my thirties,...
by follower on Thursday October 21, 2010 @03:24PM
(#1817062)
Ah, your use of the collections module prompted me to read the docs for it (who would've guessed :) ) which leads to a revision of my other response:
import string, collections
s = "your text here"
print collections.Counter([i for i in s.lower()
if i in string.letters]).most_common(3)[-1]
Re:Ask HN: starting a career in software dev in my thirties,...
by PakG1 on Thursday October 21, 2010 @12:50PM
(#1816310)
That's true enough. But fact is, if someone's ignorant of modulus, I'd say that's scary. Knowing modulus doesn't necessarily demonstrate you have a great grasp of programming. But I think not knowing modulus demonstrates that there's a higher probability you know very little. And fact is, FizzBuzz also filters out all the people who know modulus, but can't figure it out anyway. You definitely don't want those people.
Re:Ask HN: starting a career in software dev in my thirties,...
by paulnelligan on Monday October 25, 2010 @05:13AM
(#1829113)
I didn't realise that some programmers didn't know modulus ... really??
Re:Ask HN: starting a career in software dev in my thirties,...
by BrandonM on Thursday October 21, 2010 @01:46PM
(#1816602)
> write a program which calculates the sum of all numbers between 1 and 1,000 whose digits sum up to 7
My Python answer:
sum(x for x in xrange(2, 1000) if sum(int(y) for y in str(x)) == 7)
# 9324
Anyone have improvements on that?(I used the range (1, 1000) as opposed to [1, 1000], but it doesn't make a difference since the edge values don't have digits that sum to 7.)
Re:Ask HN: starting a career in software dev in my thirties,...
by kenjackson on Thursday October 21, 2010 @02:28PM
(#1816806)
There are a lot of micro improvements you can do to this. For example, after you find sum() == 7, you can increment x by 9 (since the largest digit in the ones column can be 7, and this takes a deficit of two digits to get back to the rollover -- and then 7 more to get a sum of 7 again).
And frankly I'm not sure if its faster to convert to string and back to int, or if it would be better to just use modulo arithmitic to get the values associated with the ones/tens/hundreds.
In any case, what you have is certainly readable though.
Re:Ask HN: starting a career in software dev in my thirties,...
by BrandonM on Thursday October 21, 2010 @02:51PM
(#1816921)
I mainly meant "improve" in terms of conciseness and readability. I didn't see any need for optimization because the snippet runs instantly:
$ time python -c "print sum(x for x in xrange(2, 1000) if sum(int(y) for y in str(x)) == 7)"
9324
real 0m0.138s
user 0m0.012s
sys 0m0.012s
Re:Ask HN: starting a career in software dev in my thirties,...
by follower on Thursday October 21, 2010 @03:13PM
(#1817013)
> Or write a program which takes this post as input and tells me what the 3rd most common letter used was.
I'm going for 'a'.
And, according to http://en.wikipedia.org/wiki/File:English_letter_frequency_(... (via http://en.wikipedia.org/wiki/Letter_frequency) your four most frequently used letters match the table for English.
Tested in Python 2.7:
s = "your text here"
import string
d = {}
for i in list(s.lower()):
if i in string.letters:
d[i] = d.setdefault(i, 0) + 1
print sorted(d.keys(), cmp = lambda x, y : cmp(d[x], d[y]), reverse = True)[2]
Edit: See my later response for something using the 'collections' module.
Re:Ask HN: starting a career in software dev in my thirties,...
by ciupicri on Friday October 22, 2010 @11:34AM
(#1820025)
You don't need to do `list(s.lower())`, you can already iterate over a string. Also, instead of converting the whole string to a new lowercase one, which means allocating "a lot" of memory, you can convert each character separately as needed.
If you prefer, you can substitute a similar problem which doesn't require even fourth grade math. Here's one: write a program which calculates the sum of all numbers between 1 and 1,000 whose digits sum up to 7. Or write a program which takes this post as input and tells me what the 3rd most common letter used was.