|
![](/i/fill.gif) |
On 12/5/2012 1:27 PM, Orchid Win7 v1 wrote:
>
> I would expect that for this example question, you would be very
> hard-pressed to construct a program inefficient enough to be considered
> "too slow". ;-)
That's because you're not thinking like someone who has no idea what
they're doing.
Take for instance the following algorithm, which I *tried* to make mimic
the sort of answer a clueless person might stumble and grope their way
to after hours of trial and error. I couldn't manage make the coding
style quite bad enough to be realistic, but you'll get the idea. I did
at least manage to put in a bug or two which ends up working due to
blind luck.
example of use:
sortInts("5000 353 1 145 145 145 354 23 16564")
the (horrible horrible) code:
def sortInts(S):
i = 0
count = 0
while count < countSpaces(S)+1:
n = countInStr(i, S)
if n > 0:
for j in range(n):
print i
count += 1
i += 1
def countInStr(n, S):
S = " " + S + " "
count = 0
for i in range(len(S)):
for j in range(i,len(S)):
if S[i-1:j+1] == " " + str(n) + " ":
count += 1
return count
def countSpaces(S):
count = 0
wasLastSpace = False
for i in range(len(S)):
if S[i] == " ":
if wasLastSpace == False:
count += 1
wasLastSpace = True
else:
wasLastSpace = False
return count
Post a reply to this message
|
![](/i/fill.gif) |