Skip to content

Commit

Permalink
day 3 greedy chocolate eating
Browse files Browse the repository at this point in the history
  • Loading branch information
yozaam committed Oct 23, 2020
1 parent 649f0dc commit 537bcbd
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 35 deletions.
1 change: 0 additions & 1 deletion day2_count_single_occurance.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@
print('Answer:')
print('2 has ',count,'occurances')

# If you are same as your reverse, you are a palindrome
18 changes: 18 additions & 0 deletions day3_greedy_chocolate_edges.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Required concepts: https://www.w3schools.com/python/python_intro.asp lists

print('Question: simulate game where raj can pick the chocolate from either edge of list, he will always pick the bigger one')

chocolates = [1,3,4,5,6]

while len(chocolates) > 1:
#check who is bigger first or last?
if chocolates[0] > chocolates[-1]:
# what if first is bigger?
eat = chocolates.pop(0)
else:
eat = chocolates.pop()
print('Eating ',eat)

print('Answer:')
print('Left with chocolate',chocolates[0])

40 changes: 40 additions & 0 deletions day3_run_length_decoding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Required concepts: https://www.w3schools.com/python/python_intro.asp loops and strings

print('Question: Restore a string from its run length encoding, where each char, followed by its consecutive frequency')


string = 'a2b3c4'
res = ''
# we want to encode as, val,consecutive fre
# aaabbbcccc is the desired output

for i in range(1,len(string),2):
# visit only digits ;) on odd indices
for _ in range(int(string[i])):
res += string[i-1]
# keep adding the prev character as many times as required


print('Answer:')
print(res)

print('Question: Restore a string from its run length encoding, where each char, followed by its consecutive frequency (may be ≥1 digit)')
# method if frequency is more than one digit!
string = 'a2b3c11'
res = ''

freq = 0
cur_element = ''
for i in range(len(string)):
# visit only digits ;)
if string[i].isdigit():
freq = freq * 10 + int(string[i])
else:
res += cur_element * freq
freq = 0
cur_element = string[i]

res += cur_element * freq

print('Answer:')
print(res)
46 changes: 12 additions & 34 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,18 @@
# Required concepts: https://www.w3schools.com/python/python_intro.asp loops and strings
# Required concepts: https://www.w3schools.com/python/python_intro.asp lists

print('Question: Restore a string from its run length encoding, where each char, followed by its consecutive frequency')
print('Question: simulate game where raj can pick the chocolate from either edge of list, he will always pick the bigger one')

chocolates = [1,3,4,5,6]

string = 'a2b3c4'
res = ''
# we want to encode as, val,consecutive fre
# aaabbbcccc is the desired output

for i in range(1,len(string),2):
# visit only digits ;) on odd indices
for _ in range(int(string[i])):
res += string[i-1]
# keep adding the prev character as many times as required


print('Answer:')
print(res)

print('Question: Restore a string from its run length encoding, where each char, followed by its consecutive frequency (may be ≥1 digit)')
# method if frequency is more than one digit!
string = 'a2b3c11'
res = ''

freq = 0
cur_element = ''
for i in range(len(string)):
# visit only digits ;)
if string[i].isdigit():
freq = freq * 10 + int(string[i])
while len(chocolates) > 1:
#check who is bigger first or last?
if chocolates[0] > chocolates[-1]:
# what if first is bigger?
eat = chocolates.pop(0)
else:
res += cur_element * freq
freq = 0
cur_element = string[i]

res += cur_element * freq
eat = chocolates.pop()
print('Eating ',eat)

print('Answer:')
print(res)
print('Left with chocolate',chocolates[0])

0 comments on commit 537bcbd

Please sign in to comment.