Skip to content

Commit 41821ef

Browse files
More exercises!
1 parent 614d2f1 commit 41821ef

File tree

76 files changed

+736
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+736
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

ex-037-hello-world/hello-world.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# course: python self training
2+
# exercise: 1
3+
# date: Oct 3 2020
4+
# username: shinigami
5+
# name: Cristiano Cavo
6+
# description: the classic hello world
7+
# filename: hello-world.py
8+
# notes:
9+
# - No need to import stdio libraries
10+
# - Run it as: `python3 hello-world.py` or simply `py h*` if you are too lazy and aliased py in your .zshrc
11+
12+
def helloWorld():
13+
print("Hello, World!")
14+
15+
helloWorld()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# course: python self training
2+
# exercise: 2
3+
# date: Oct 3 2020
4+
# username: shinigami
5+
# name: Cristiano Cavo
6+
# description: learn how to declare variables of different types and how to formet-print them
7+
# filename: types-and-format.py
8+
9+
# declare an integer
10+
apples = 5
11+
12+
# declare a string
13+
auntie = "Auntie Cleo"
14+
15+
# delcare a float
16+
price = 2.7
17+
18+
19+
def myFormattedPrint(a, b, c):
20+
print("{0} bought {1} apples and paid {2} euros for them".format(a, b, c))
21+
22+
myFormattedPrint(auntie, apples, price)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# course: python self training
2+
# exercise: 3
3+
# date: Oct 3 2020
4+
# username: shinigami
5+
# name: Cristiano Cavo
6+
# description: learn how to make conditional ifs, be aware of colons, indentation! and elif!
7+
# filename: max-from-2-and-3-integers.py
8+
9+
# declare some integers
10+
apples = 2
11+
watermelons = 3
12+
lemons = 5
13+
14+
# compare apples and lemons, give the max
15+
if(apples > lemons):
16+
print("we have more apples than lemons")
17+
else:
18+
print("we have more lemons than apples")
19+
20+
# compare all!
21+
if(apples > lemons and apples > watermelons):
22+
print("we have many apples!")
23+
elif(lemons > apples and lemons > watermelons):
24+
print("we have many lemons!")
25+
else:
26+
print("we have many watermelons!")

ex-040-find-vowels/find-vowels.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# course: python self training
2+
# exercise: 4
3+
# date: Oct 4 2020
4+
# username: shinigami
5+
# name: Cristiano Cavo
6+
# description: discover python potential over string processing and the 'in' keyword
7+
# filename: find-vowels.py
8+
9+
# decalre a string
10+
myRandomString = "qwertyuiopasdfghjklzxcvbnm"
11+
12+
def findVowels(str):
13+
vowels = "aeiouy"
14+
for carat in str:
15+
if carat in vowels:
16+
print(carat,"is a vowel")
17+
else:
18+
print(carat,"is a consonant")
19+
20+
findVowels(myRandomString)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# course: python self training
2+
# exercise: 5
3+
# date: Oct 4 2020
4+
# username: shinigami
5+
# name: Cristiano Cavo
6+
# description: learn how to declare lists and how to iterate them
7+
# filename: unstoppable-summation.py
8+
9+
# declare a list of numbers
10+
myList = [1, 1, 2, 4]
11+
12+
# print the list
13+
print(myList)
14+
15+
# declare a summatory that calculate the sum of all items of a given list
16+
def unstoppableSummation(numbers):
17+
result = 0
18+
for number in numbers:
19+
result = result + number
20+
return result
21+
22+
print("the overall sum of the items in the list above results:",unstoppableSummation(myList))
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# course: python self training
2+
# exercise: 6
3+
# date: Oct 4 2020
4+
# username: shinigami
5+
# name: Cristiano Cavo
6+
# description: learn some useful python tricks
7+
# filename: reverse-string.py
8+
9+
# declare some strings
10+
str1 = "apples"
11+
str2 = "peches, pommes et poitres"
12+
str3 = "radar"
13+
14+
# define the string reverter function
15+
def stringReverter(str):
16+
return str[::-1]
17+
18+
print(str1,"> reverted is >",stringReverter(str1))
19+
print(str2,"> reverted is >",stringReverter(str2))
20+
print(str3,"> reverted is >",stringReverter(str3))

ex-043-palindrome/find-palindrome.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# course: python self training
2+
# exercise: 7
3+
# date: Oct 4 2020
4+
# username: shinigami
5+
# name: Cristiano Cavo
6+
# description: something boring on find palindrome strings
7+
# filename: find-palindrome.py
8+
9+
# define some strings
10+
str1 = "apples"
11+
str2 = "peches, pommes et poitres"
12+
str3 = "radar"
13+
str4 = "never odd or even"
14+
str5 = "kayak"
15+
str6 = "not a palindrome"
16+
17+
# define the string reverter function
18+
def stringReverter(str):
19+
return str[::-1]
20+
21+
# define palindrome finder
22+
def isPalindrome(str):
23+
if(str == stringReverter(str)):
24+
return True
25+
else:
26+
return False
27+
28+
print(str1,"is palindrome?",isPalindrome(str1))
29+
print(str2,"is palindrome?",isPalindrome(str2))
30+
print(str3,"is palindrome?",isPalindrome(str3))
31+
print(str4,"is palindrome?",isPalindrome(str4))
32+
print(str5,"is palindrome?",isPalindrome(str5))
33+
print(str6,"is palindrome?",isPalindrome(str6))

ex-044-histogram/histogram.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# course: python self training
2+
# exercise: 8
3+
# date: Sat Oct 4 2020
4+
# username: shinigami
5+
# name: Cristiano Cavo
6+
# description: draw a CLI histogram starting from a list of numebrs
7+
# filename: cli-histogram.py
8+
9+
# declare a list of numbers
10+
numbers = [1, 8, 4, 7, 3, 6]
11+
12+
# define the histogram drawer
13+
def histogram(list, symbol):
14+
for val in list:
15+
print("[{0}]".format(val),"> ", end="")
16+
for i in range(val):
17+
print(symbol, end="")
18+
print("")
19+
20+
histogram(numbers, "o")
21+
histogram(numbers, "#")
22+
histogram(numbers, "x ")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# course: python self training
2+
# exercise: 9
3+
# date: Oct 6 2020
4+
# username: shinigami
5+
# name: Cristiano Cavo
6+
# description: this is a program that, passed as a parameter a list of integers, outputs the greater of the numbers contained in the list.
7+
# filename: number-list-find-max.py
8+
9+
# declare a list of random numbers from 0 to 100 having 50 elements
10+
11+
import random # import random library
12+
numbers = random.sample(range(0, 100), 50) # generate 50 random numbers between 0 and 100
13+
print(numbers) # print the generated list
14+
maxNumber = max(numbers) # pick the greatest number in the list
15+
print("the max in the list is:",maxNumber)

ex-046-rovarspraket/rovarspraket.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# course: python self training
2+
# exercise: 10
3+
# date: Oct 6 2020
4+
# username: shinigami
5+
# name: Cristiano Cavo
6+
# description: implement the famous swedish grammar game
7+
# filename: rovarspraket.py
8+
9+
# define some words
10+
words = ["cane", "melograno", "assomigliare", "lingua", "rovarspraket", "hypoteses", "majestic"]
11+
12+
# define vowels
13+
vowels = "aeiouy"
14+
15+
# define if a carat is a vowel
16+
def isVowel(char):
17+
if(char in vowels):
18+
return True
19+
else:
20+
return False
21+
22+
# define translater
23+
def rovarspraket(word):
24+
translatedWord = ""
25+
for charIndex in range(0, len(word)):
26+
char = word[charIndex]
27+
if(isVowel(char)):
28+
translatedWord = translatedWord + char
29+
else:
30+
translatedWord = translatedWord + char + "o" + char
31+
return translatedWord
32+
33+
# print the results
34+
for word in words:
35+
print(rovarspraket(word))

ex-047-char-counter/char-counter.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# course: python self training
2+
# exercise: 11
3+
# date: Oct 6 2020
4+
# username: shinigami
5+
# name: Cristiano Cavo
6+
# description: implement a function that returns the occurences of charaters in a give string
7+
# filename: char-counter.py
8+
9+
# define some strings
10+
s1 = "apples"
11+
s2 = "il rintocco della campana di gion"
12+
s3 = "heike monogatari"
13+
s4 = "appomattox"
14+
15+
# define the char counter
16+
def charCounter(s):
17+
occurences = {}
18+
for c in s:
19+
if(c != " "):
20+
if c in occurences:
21+
occurences[c] += 1
22+
else:
23+
occurences[c] = 1
24+
return occurences
25+
26+
# do it
27+
print(charCounter(s1))
28+
print(charCounter(s2))
29+
print(charCounter(s3))
30+
print(charCounter(s4))
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# course: python self training
2+
# exercise: 12
3+
# date: Oct 6 2020
4+
# username: shinigami
5+
# name: Cristiano Cavo
6+
# description: implement a program be able to calculate area of a circle from given radius, area of a square from given edge, a rectangle form given edges and of a equilateral triangle froma given edge
7+
# filename: char-counter.py
8+
9+
# define area of a circle with given radius
10+
def circleArea(r):
11+
area = 3.14 * r * r
12+
return area
13+
14+
def squareArea(s):
15+
area = s * s
16+
return area
17+
18+
def rectangleArea(a, b):
19+
area = a * b
20+
return area
21+
22+
import math
23+
def triangleArea(s):
24+
area = (math.sqrt(3)/4) * s * s
25+
return area
26+
27+
# define main menu
28+
def menu():
29+
print("""
30+
Welcome to Simple CLI Geometry!
31+
Make a choice:
32+
[1] circlea area
33+
[2] square area
34+
[3] rectangle area
35+
[4] triangle area
36+
""")
37+
c = int(input('=> '))
38+
if(c == 1):
39+
circleRadius = float(input("give the circle radius: "))
40+
print("area of a circle with radius length of",circleRadius,"is: ",circleArea(circleRadius))
41+
elif(c == 2):
42+
squareSide = float(input("give the square side: "))
43+
print("area of a square with side length of",squareSide,"is: ",squareArea(squareSide))
44+
elif(c == 3):
45+
rectangleSideA = float(input("give the rectangle side A: "))
46+
rectangleSideB = float(input("give the rectangle side B: "))
47+
print("area of a rectangle with side A of",rectangleSideA,"and side B of",rectangleSideB,"is: ",rectangleArea(rectangleSideA, rectangleSideB))
48+
elif(c == 4):
49+
triangleSide = float(input("give the triangle side: "))
50+
print("area of a triangle with side length of",triangleSide,"is: ",triangleArea(triangleSide))
51+
else:
52+
print("undefined command, exit")
53+
54+
menu()
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# course: python self training
2+
# exercise: 13
3+
# date: Oct 11 2020
4+
# username: shinigami
5+
# name: Cristiano Cavo
6+
# description: write a cli program capable of generate different types of password
7+
# filename: password-generator.py
8+
9+
# define digits
10+
digits = "0987654321"
11+
12+
# define alphabet
13+
alphabet = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"
14+
15+
# define alphanumeric set
16+
alphanumeric = digits + alphabet
17+
18+
# define symbols
19+
symbols = alphanumeric + "\\!£$%&/()=?"
20+
21+
# define menu
22+
def menu():
23+
print("""Welcome to the password generator:
24+
[1] generate a numeric only passowrd")
25+
[2] generate an alphabetcial passowrd")
26+
[3] generate an alphanumeric passowrd")
27+
[4] generate a ascii passowrd""")
28+
c = int(input("> "))
29+
l = int(input("password length?\n> "))
30+
if(c == 1):
31+
generator(digits, l)
32+
elif(c == 2):
33+
generator(alphabet, l)
34+
elif(c == 3):
35+
generator(alphanumeric, l)
36+
elif(c == 4):
37+
generator(symbols, l)
38+
else:
39+
print("command not found")
40+
return
41+
print("your password has been saved in the clipboard")
42+
43+
# define generator
44+
import random
45+
import pyperclip
46+
def generator(set, length):
47+
password = ""
48+
for i in range(length):
49+
password += set[int(random.randrange(len(set)))]
50+
pyperclip.copy(password)
51+
52+
53+
menu()

0 commit comments

Comments
 (0)