From dbffd42bb79726b8e5a5094d13e5c0c47eac05b4 Mon Sep 17 00:00:00 2001 From: ekod77677 Date: Sun, 2 Oct 2022 15:39:42 +0300 Subject: [PATCH 1/5] week2 answer1 done --- week2_answer1.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 week2_answer1.py diff --git a/week2_answer1.py b/week2_answer1.py new file mode 100644 index 0000000..497aa60 --- /dev/null +++ b/week2_answer1.py @@ -0,0 +1,21 @@ +# Write a program that takes two inputs; one of them is a list and the other is a number, and returns +# the list obtained by shifting the elements in the list n places to the right (left) +# when n is positive (negative). Use wrap-around: if an element is shifted beyond the end of the list, +# then continue to shift starting at the beginning of the list. + +# ``` +# Example +# Inputs>>> [1, 2, 3, 4, 5], 2 +# Output>>> [4, 5, 1, 2, 3] +# Inputs>>> [1, 2, 3, 4, 5], -2 +# Output>>> [3, 4, 5, 1, 2] +# ``` + +def rotate (lst, n): + return lst[n:] + lst[:n] +# if n>=0: +# shift_order(lst, n) +# else: +# shift_order(lst, n) +# return lst +print (rotate([1, 2, 3, 4, 5], -2)) \ No newline at end of file From baa6aeb270b3c7736ece2e718f8e710fc88b060d Mon Sep 17 00:00:00 2001 From: ekod77677 Date: Sun, 2 Oct 2022 16:10:27 +0300 Subject: [PATCH 2/5] week2 question2 done --- week2_answer1.py | 8 ++------ week2_answer2.py | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 week2_answer2.py diff --git a/week2_answer1.py b/week2_answer1.py index 497aa60..f9e6147 100644 --- a/week2_answer1.py +++ b/week2_answer1.py @@ -12,10 +12,6 @@ # ``` def rotate (lst, n): - return lst[n:] + lst[:n] -# if n>=0: -# shift_order(lst, n) -# else: -# shift_order(lst, n) -# return lst + return reversed (lst[n:] + lst[:n]) + print (rotate([1, 2, 3, 4, 5], -2)) \ No newline at end of file diff --git a/week2_answer2.py b/week2_answer2.py new file mode 100644 index 0000000..66b3fd6 --- /dev/null +++ b/week2_answer2.py @@ -0,0 +1,24 @@ +# Write a code snippet that inputs a sentence from the user, then uses a dictionary to summarize the number of +# occurrences of each i. Ignore case, ignore blanks and assume the user does not enter any punctuation. +# Display a two-column table of the letters and their counts with the letters in sorted order. + +# ``` +# Example +# Input >>> "This is a sample text with several words This is more sample text with some different words" +# Output >>> +# [('a', 4), ('d', 3), ('e', 10), ('f', 2), ('h', 4), ('i', 7), ('l', 3), ('m', 4), ('n', 1), ('o', 4), ('p', 2), ('r', 5), ('s', 10), ('t', 9), ('v', 1), ('w', 4), ('x', 2)] +# ``` + +def count_letters(text): + result = {} + text = text.lower() + # Go through each letter in the text + for i in text: + # Check if the i needs to be counted or not + if i.isalpha() : + # Add or increment the value in the dictionary + count = text.count(i) + result[i] = count + return result + +print(count_letters("This is a sample text")) From 80604483c43bf1c8bbfa28cbe8466958ec07ea99 Mon Sep 17 00:00:00 2001 From: ekod77677 Date: Sun, 2 Oct 2022 17:22:55 +0300 Subject: [PATCH 3/5] week2 answer3 --- week2_answer3.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 week2_answer3.py diff --git a/week2_answer3.py b/week2_answer3.py new file mode 100644 index 0000000..00e4746 --- /dev/null +++ b/week2_answer3.py @@ -0,0 +1,40 @@ +# Write a program that takes in two words as input and returns a list containing three elements, +# in the following order: +# - Shared letters between two words. + +# - Letters unique to word 1. + +# - Letters unique to word 2. + +# Each element of the output should have unique letters, and should be alphabetically sorted. +# Use set operations. The strings will always be in lowercase and will not contain any punctuation. +# ``` +# Example +# Input1>>> "sharp" +# Input2>>> "soap" +# Output>>> ['aps', 'hr', 'o'] +# ``` +# def is_anagram(str1, str2): +# return sorted(str1) == sorted(str2) +# print (is_anagram('sharp', 'soap')) +def Common_Uncommon_Words(str1, str2): + l1 = [] + l2 = [] + l3 = [] + for i in str1: + if (i in str2): + l1.append(i) + elif (i not in str2): + l2.append(i) + for j in str2: + if (j not in str1): + l3.append(j) + return [''.join(l1)] + [''.join(l2)] + [''.join(l3)] + + +print(Common_Uncommon_Words('sharp', 'soap')) + + + + + From 6b623726e0408a725dad5e5af2fdae8cc7484173 Mon Sep 17 00:00:00 2001 From: ekod77677 Date: Tue, 4 Oct 2022 20:03:16 +0300 Subject: [PATCH 4/5] week2 3 question done --- week2_answer1.py | 19 ++++++++++++++++--- week2_answer3.py | 3 --- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/week2_answer1.py b/week2_answer1.py index f9e6147..6a559ac 100644 --- a/week2_answer1.py +++ b/week2_answer1.py @@ -11,7 +11,20 @@ # Output>>> [3, 4, 5, 1, 2] # ``` -def rotate (lst, n): - return reversed (lst[n:] + lst[:n]) +# def rotate (lst, n): +# return lst[n:]+lst[:n] + +# print (rotate([1, 2, 3, 4, 5], 2)) +def shift(lis, n): + + if 0n> -1 * len(lis): + return lis[len(lis) + n - 1:] + lis[:n - 1] + else: + return lis -print (rotate([1, 2, 3, 4, 5], -2)) \ No newline at end of file + + + +print(shift([1, 2, 3, 4, 5], -2)) \ No newline at end of file diff --git a/week2_answer3.py b/week2_answer3.py index 00e4746..8e38937 100644 --- a/week2_answer3.py +++ b/week2_answer3.py @@ -14,9 +14,6 @@ # Input2>>> "soap" # Output>>> ['aps', 'hr', 'o'] # ``` -# def is_anagram(str1, str2): -# return sorted(str1) == sorted(str2) -# print (is_anagram('sharp', 'soap')) def Common_Uncommon_Words(str1, str2): l1 = [] l2 = [] From 43721638da4588375208d6d7699b4c01dca47ef3 Mon Sep 17 00:00:00 2001 From: ekod77677 Date: Tue, 4 Oct 2022 20:30:21 +0300 Subject: [PATCH 5/5] latest version of week-2 3 questions answers --- week2_answer1.py | 22 +--------------------- week2_answer2.py | 11 ----------- week2_answer3.py | 16 ---------------- 3 files changed, 1 insertion(+), 48 deletions(-) diff --git a/week2_answer1.py b/week2_answer1.py index 6a559ac..1262aae 100644 --- a/week2_answer1.py +++ b/week2_answer1.py @@ -1,20 +1,3 @@ -# Write a program that takes two inputs; one of them is a list and the other is a number, and returns -# the list obtained by shifting the elements in the list n places to the right (left) -# when n is positive (negative). Use wrap-around: if an element is shifted beyond the end of the list, -# then continue to shift starting at the beginning of the list. - -# ``` -# Example -# Inputs>>> [1, 2, 3, 4, 5], 2 -# Output>>> [4, 5, 1, 2, 3] -# Inputs>>> [1, 2, 3, 4, 5], -2 -# Output>>> [3, 4, 5, 1, 2] -# ``` - -# def rotate (lst, n): -# return lst[n:]+lst[:n] - -# print (rotate([1, 2, 3, 4, 5], 2)) def shift(lis, n): if 0>> "This is a sample text with several words This is more sample text with some different words" -# Output >>> -# [('a', 4), ('d', 3), ('e', 10), ('f', 2), ('h', 4), ('i', 7), ('l', 3), ('m', 4), ('n', 1), ('o', 4), ('p', 2), ('r', 5), ('s', 10), ('t', 9), ('v', 1), ('w', 4), ('x', 2)] -# ``` - def count_letters(text): result = {} text = text.lower() diff --git a/week2_answer3.py b/week2_answer3.py index 8e38937..fac803a 100644 --- a/week2_answer3.py +++ b/week2_answer3.py @@ -1,19 +1,3 @@ -# Write a program that takes in two words as input and returns a list containing three elements, -# in the following order: -# - Shared letters between two words. - -# - Letters unique to word 1. - -# - Letters unique to word 2. - -# Each element of the output should have unique letters, and should be alphabetically sorted. -# Use set operations. The strings will always be in lowercase and will not contain any punctuation. -# ``` -# Example -# Input1>>> "sharp" -# Input2>>> "soap" -# Output>>> ['aps', 'hr', 'o'] -# ``` def Common_Uncommon_Words(str1, str2): l1 = [] l2 = []