From 649f0dc843b04fe90dc2c58596e1285d23b047ea Mon Sep 17 00:00:00 2001 From: Yohaan Date: Fri, 23 Oct 2020 03:39:05 +0000 Subject: [PATCH] day 3 run length --- day3_reverse_no_inbuilt.py | 21 ++++++++++++++++++ day3_run_length_encoding.py | 36 ++++++++++++++++++++++++++++++ main.py | 44 ++++++++++++++++++++++++++++--------- 3 files changed, 91 insertions(+), 10 deletions(-) create mode 100644 day3_reverse_no_inbuilt.py create mode 100644 day3_run_length_encoding.py diff --git a/day3_reverse_no_inbuilt.py b/day3_reverse_no_inbuilt.py new file mode 100644 index 0000000..2ca7f13 --- /dev/null +++ b/day3_reverse_no_inbuilt.py @@ -0,0 +1,21 @@ +# Required concepts: https://www.w3schools.com/python/python_intro.asp while loops and lists + +print('Question: Reverse a list without inbuilt function/slicing') + +li = [1,2,3,4,5] +# we need to make the first element the last +# and then keep swapping them :D +left, right = 0, len(li)-1 + +while left < right: + li[left],li[right] = li[right],li[left] + left +=1 + right -=1 + +#single line +li = [1,2,3,4,5] +for left in range(len(li)//2): + li[left],li[len(li)-left-1] = li[len(li)-left-1],li[left] + +print('Answer:') +print(li) \ No newline at end of file diff --git a/day3_run_length_encoding.py b/day3_run_length_encoding.py new file mode 100644 index 0000000..186e199 --- /dev/null +++ b/day3_run_length_encoding.py @@ -0,0 +1,36 @@ +# Required concepts: https://www.w3schools.com/python/python_intro.asp loops and strings + +print('Question: Convert a string into its run length encoding, each char, followed by its consecutive frequency') + +string = 'aaabbbcccc' +res = '' +# we want to encode as, val,consecutive fre +# a3b3c4 is the desired output + +cur_freq = 0 +for i in range(len(string)-1): + if string[i] == string[i+1]: + cur_freq += 1 + else: + res += string[i] + str(cur_freq) + cur_freq = 1 +res += string[-1] + str(cur_freq) + +print('Answer:') +print(res) + +""" +alternate easy method: +cur_freq = 0 +cur_element = string[0] + +for element in string: + if element == cur_element: + cur_freq +=1 + else: + res += cur_element + str(cur_freq) + cur_freq = 1 + cur_element = element + +res += cur_element + str(cur_freq) +""" \ No newline at end of file diff --git a/main.py b/main.py index 295f6bf..d325a9e 100644 --- a/main.py +++ b/main.py @@ -1,16 +1,40 @@ -# Required concepts: https://www.w3schools.com/python/python_intro.asp Everything till for loops +# Required concepts: https://www.w3schools.com/python/python_intro.asp loops and strings -print('Question: repeat every character twice') +print('Question: Restore a string from its run length encoding, where each char, followed by its consecutive frequency') -li = ['a' , 'b' , 'c' , 'd'] -# we want ['aa' , 'bb' , 'cc' , 'dd'] -for i in range(len(li)): - li[i] = li[i]*2 - # all elements repeat twice so *2 +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 + -#single line let's repeat them again -result = [letter*2 for letter in li] print('Answer:') -print(result) +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