From 60a4ec8d8022d3f225cea23c1db31b818cd53411 Mon Sep 17 00:00:00 2001 From: Salih Enboylu Date: Tue, 4 Oct 2022 19:32:09 +0200 Subject: [PATCH 1/2] Q1 Done --- Q1.py | 24 ++++++++++++++++++++++++ README.md | 7 +++++-- 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 Q1.py diff --git a/Q1.py b/Q1.py new file mode 100644 index 0000000..42603b0 --- /dev/null +++ b/Q1.py @@ -0,0 +1,24 @@ +# ### 1. +# 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 shiftfunction(mylist, mynumber): + + if 0mynumber> -1 * len(mylist): + return mylist[len(mylist) + mynumber - 1:] + mylist[:mynumber - 1] + else: + return mylist + + + + +print(shiftfunction([1,2,3,4,5], 2)) \ No newline at end of file diff --git a/README.md b/README.md index 7dc5a08..96afa40 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Class7-Python-Module-Week2 + From 3270128f2a937fed58880576c2233172ef222f97 Mon Sep 17 00:00:00 2001 From: Salih Enboylu Date: Tue, 4 Oct 2022 19:52:34 +0200 Subject: [PATCH 2/2] Q1, Q2, and Q3 done --- Q1.py | 2 +- Q2.py | 21 +++++++++++++++++++++ Q3.py | 27 +++++++++++++++++++++++++++ README.md | 56 ------------------------------------------------------- 4 files changed, 49 insertions(+), 57 deletions(-) create mode 100644 Q2.py create mode 100644 Q3.py delete mode 100644 README.md diff --git a/Q1.py b/Q1.py index 42603b0..7732d24 100644 --- a/Q1.py +++ b/Q1.py @@ -21,4 +21,4 @@ def shiftfunction(mylist, mynumber): -print(shiftfunction([1,2,3,4,5], 2)) \ No newline at end of file +print(shiftfunction([1,2,3,4], 2)) \ No newline at end of file diff --git a/Q2.py b/Q2.py new file mode 100644 index 0000000..05caa59 --- /dev/null +++ b/Q2.py @@ -0,0 +1,21 @@ +# Write a code snippet that inputs a sentence from the user, then uses a dictionary to summarize the number of occurrences of each letter. 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)] +# ``` + +example = 'This an example sentence'*3 + +def extract_characters(example): + examplelist = list(example) + char = {} + for cha in set(examplelist): + char[cha] = examplelist.count(cha) + return char + + +char = extract_characters(example) +print(char) \ No newline at end of file diff --git a/Q3.py b/Q3.py new file mode 100644 index 0000000..f3160de --- /dev/null +++ b/Q3.py @@ -0,0 +1,27 @@ +# ### 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 two_words(w1, w2): + w1=set(w1) + w2=set(w2) + lis=[] + + lis.append ("".join (sorted (w1& w2 )) ) + lis.append ("".join (sorted (w1- w2 )) ) + lis.append ("".join (sorted (w2-w1 )) ) + + return lis +print (two_words("sharp","soap") ) \ No newline at end of file diff --git a/README.md b/README.md deleted file mode 100644 index 96afa40..0000000 --- a/README.md +++ /dev/null @@ -1,56 +0,0 @@ -