diff --git a/day2_count_single_occurance.py b/day2_count_single_occurance.py index 45cee17..8d13e74 100644 --- a/day2_count_single_occurance.py +++ b/day2_count_single_occurance.py @@ -16,4 +16,3 @@ print('Answer:') print('2 has ',count,'occurances') -# If you are same as your reverse, you are a palindrome diff --git a/day3_greedy_chocolate_edges.py b/day3_greedy_chocolate_edges.py new file mode 100644 index 0000000..94c0095 --- /dev/null +++ b/day3_greedy_chocolate_edges.py @@ -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]) + diff --git a/day3_run_length_decoding.py b/day3_run_length_decoding.py new file mode 100644 index 0000000..d325a9e --- /dev/null +++ b/day3_run_length_decoding.py @@ -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) \ No newline at end of file diff --git a/main.py b/main.py index d325a9e..94c0095 100644 --- a/main.py +++ b/main.py @@ -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) \ No newline at end of file +print('Left with chocolate',chocolates[0]) +