-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
70 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,3 @@ | |
print('Answer:') | ||
print('2 has ',count,'occurances') | ||
|
||
# If you are same as your reverse, you are a palindrome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
|