diff --git a/basic/list1.py b/basic/list1.py index 4e6171c..7c91074 100755 --- a/basic/list1.py +++ b/basic/list1.py @@ -22,7 +22,13 @@ # Note: python does not have a ++ operator, but += works. def match_ends(words): # +++your code here+++ - return + count = 0 + for string in words: + if len(string) >= 2 and string[0] == string[-1]: + count = count +1 + else: + continue + return(count) # B. front_x @@ -34,7 +40,19 @@ def match_ends(words): # before combining them. def front_x(words): # +++your code here+++ - return + list_x = [] + list_no_x = [] + for string in words: + if string[0] == 'x': + list_x.append(string) + else: + list_no_x.append(string) + list_x.sort() + list_no_x.sort() + + new_list = list_x + list_no_x + + return(new_list) # C. sort_last @@ -45,8 +63,29 @@ def front_x(words): # Hint: use a custom key= function to extract the last element form each tuple. def sort_last(tuples): # +++your code here+++ - return + new_tuples = sorted(tuples, key =lambda tuples: tuples[-1] ) + return new_tuples + + #last_element_list = [] + #for string in tuples: + #last_element_list.append(string[-1]) + + # last_element_list.sort() + #print(last_element_list) + #j = 0 + #final_list = [] + #for nums in last_element_list: + #for element in tuples: + # if element[-1] == nums: + # final_list.append(element) + # else: + # continue + #return(final_list) + + + # for last_element in last_element_list: + # if last_element = # Simple provided test() function used in main() to print # what each function returns vs. what it's supposed to return. diff --git a/basic/list2.py b/basic/list2.py index f8e65da..d65121c 100755 --- a/basic/list2.py +++ b/basic/list2.py @@ -14,17 +14,56 @@ # modify the passed in list. def remove_adjacent(nums): # +++your code here+++ - return + + new_list = [] + for i in range(0,len(nums)): + if i == 0: + new_list.append(nums[i]) + else: + if nums[i] != new_list[-1]: + new_list.append(nums[i]) + return new_list + + + #set_nums = set(nums) + #final_list= [] + #for element in set_nums: + # final_list.append(element) + + #return(final_list) # E. Given two lists sorted in increasing order, create and return a merged # list of all the elements in sorted order. You may modify the passed in lists. # Ideally, the solution should work in "linear" time, making a single # pass of both lists. -def linear_merge(list1, list2): - # +++your code here+++ - return +#enyem +#def linear_merge(list1, list2): + # +++your code here+++ + #new_list = list1 + list2 + #new_list.sort() + #return(new_list) komplexitása: n*log(n) n helyett + +def linear_merge(list1, list2): + + final_list = [] + # Look at the two lists so long as both are non-empty. + # Take whichever element [0] is smaller. + while len(list1) and len(list2): + if list1[-1] < list2[-1]: + final_list.append(list2.pop(-1)) + else: + final_list.append(list1.pop(-1)) + #print(list1) + #print(list2) + if len(list1) ==0 : + final_list.append(list2[0]) + else: + final_list.append(list1[0]) + # Now tack on what's left + return sorted(final_list) + # Note: the solution above is kind of cute, but unforunately list.pop(0) # is not constant time with the standard python list implementation, so diff --git a/basic/string1.py b/basic/string1.py index 34d2716..b5f7a74 100755 --- a/basic/string1.py +++ b/basic/string1.py @@ -25,7 +25,10 @@ # and donuts(23) returns 'Number of donuts: many' def donuts(count): # +++your code here+++ - return + if count >= 10: + return('Number of donuts: many') + else: + return(f'Number of donuts: {count}') # B. both_ends @@ -35,7 +38,11 @@ def donuts(count): # is less than 2, return instead the empty string. def both_ends(s): # +++your code here+++ - return + if len(s) <= 2: + return('') + else: + new_string = s[0:2] + s[-2:] + return(new_string) # C. fix_start @@ -49,7 +56,25 @@ def both_ends(s): # where all instances of stra have been replaced by strb. def fix_start(s): # +++your code here+++ - return + first_letter = s[0] + for letter in s: + if letter == s[0]: + s = s.replace(letter,'*') + else: + new_string = s + + new_string = first_letter + s[1:] + return(new_string) + + + + + + + + + + #return # D. MixUp @@ -60,8 +85,12 @@ def fix_start(s): # 'dog', 'dinner' -> 'dig donner' # Assume a and b are length 2 or more. def mix_up(a, b): - # +++your code here+++ - return + a_firsttwo = a[0:2] + b_firsttwo = b[0:2] + + new_a = b_firsttwo + a[2:] + new_b = a_firsttwo + b[2:] + return(f'{new_a} {new_b}') # Provided simple test() function used in main() to print diff --git a/basic/string2.py b/basic/string2.py index 631091a..9319711 100755 --- a/basic/string2.py +++ b/basic/string2.py @@ -17,7 +17,13 @@ # Return the resulting string. def verbing(s): # +++your code here+++ - return + if len(s) >= 3: + if s[-3:] == 'ing': + return(s + 'ly') + else: + return(s + 'ing') + else: + return(s) # E. not_bad @@ -30,7 +36,18 @@ def verbing(s): # This dinner is good! def not_bad(s): # +++your code here+++ - return + find_not = s.find('not') + find_bad = s.find('bad') + if find_not < find_bad: + if len(s[find_bad+2:]) > 0: + new_string = s[0:find_not] + 'good' + s[find_bad+3:] + return(new_string) + else: + new_string = s[0:find_not] + 'good' + return(new_string) + else: + return(s) + # F. front_back @@ -42,7 +59,25 @@ def not_bad(s): # a-front + b-front + a-back + b-back def front_back(a, b): # +++your code here+++ - return + if len(a) % 2 == 0: + a_half = int(len(a)/2) + a_front = a[:a_half] + a_back = a[a_half:] + else: + a_half = int((len(a)/2)+0.5) + a_front = a[:a_half] + a_back = a[a_half:] + + if len(b) % 2 == 0 : + b_half = int(len(b)/2) + b_front = b[:b_half] + b_back = b[b_half:] + else: + b_half = int((len(b)/2)+0.5) + b_front = b[:b_half] + b_back = b[b_half:] + + return(a_front + b_front + a_back + b_back) # Simple provided test() function used in main() to print diff --git a/basic/wordcount.py b/basic/wordcount.py index a6c7c2e..a3ab5ec 100755 --- a/basic/wordcount.py +++ b/basic/wordcount.py @@ -46,6 +46,26 @@ # and builds and returns a word/count dict for it. # Then print_words() and print_top() can just call the utility function. +def word_count(filename): + word_count = {} + f = open(filename) + for line in f: + line = line.split() + + for word in line: + word = word.lower() + if word in dict: + dict[word] += 1 + else: + dict[word] = 1 + f.close() + return dict + +def print_words(filename): + word_count = word_count_dict(filename) + words = sorted(word_count.keys()) + for word in words: + print (word, word_count[word]) ### # This basic command line argument parsing code is provided and