From 271146756b88662d16da546b81bf67a9a6ed10ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Encarnaci=C3=B3n=20Ortiz?= Date: Sun, 27 May 2018 01:43:05 -0400 Subject: [PATCH 1/4] - Some exercises done! --- .vscode/launch.json | 208 ++++++++++++++++++++++++++++++++++++++ exercises/exercise-001.py | 13 +++ exercises/exercise-002.py | 18 ++++ exercises/exercise-003.py | 14 +++ exercises/exercise-004.py | 18 ++++ exercises/exercise-005.py | 20 ++++ exercises/exercise-006.py | 26 +++++ exercises/exercise-007.py | 22 ++++ exercises/exercise-008.py | 16 +++ exercises/exercise-009.py | 19 ++++ exercises/exercise-010.py | 16 +++ exercises/exercise-011.py | 18 ++++ exercises/exercise-012.py | 11 ++ 13 files changed, 419 insertions(+) create mode 100644 .vscode/launch.json create mode 100644 exercises/exercise-001.py create mode 100644 exercises/exercise-002.py create mode 100644 exercises/exercise-003.py create mode 100644 exercises/exercise-004.py create mode 100644 exercises/exercise-005.py create mode 100644 exercises/exercise-006.py create mode 100644 exercises/exercise-007.py create mode 100644 exercises/exercise-008.py create mode 100644 exercises/exercise-009.py create mode 100644 exercises/exercise-010.py create mode 100644 exercises/exercise-011.py create mode 100644 exercises/exercise-012.py diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..8d2a72c7 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,208 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python", + "type": "python", + "request": "launch", + "stopOnEntry": true, + "pythonPath": "${config:python.pythonPath}", + "program": "${file}", + "cwd": "${workspaceRoot}", + "env": {}, + "envFile": "${workspaceRoot}/.env", + "debugOptions": [ + "WaitOnAbnormalExit", + "WaitOnNormalExit", + "RedirectOutput" + ] + }, + { + "name": "PySpark", + "type": "python", + "request": "launch", + "stopOnEntry": true, + "osx": { + "pythonPath": "${env:SPARK_HOME}/bin/spark-submit" + }, + "windows": { + "pythonPath": "${env:SPARK_HOME}/bin/spark-submit.cmd" + }, + "linux": { + "pythonPath": "${env:SPARK_HOME}/bin/spark-submit" + }, + "program": "${file}", + "cwd": "${workspaceRoot}", + "env": {}, + "envFile": "${workspaceRoot}/.env", + "debugOptions": [ + "WaitOnAbnormalExit", + "WaitOnNormalExit", + "RedirectOutput" + ] + }, + { + "name": "Python Module", + "type": "python", + "request": "launch", + "stopOnEntry": true, + "pythonPath": "${config:python.pythonPath}", + "module": "module.name", + "cwd": "${workspaceRoot}", + "env": {}, + "envFile": "${workspaceRoot}/.env", + "debugOptions": [ + "WaitOnAbnormalExit", + "WaitOnNormalExit", + "RedirectOutput" + ] + }, + { + "name": "Integrated Terminal/Console", + "type": "python", + "request": "launch", + "stopOnEntry": true, + "pythonPath": "${config:python.pythonPath}", + "program": "${file}", + "cwd": "", + "console": "integratedTerminal", + "env": {}, + "envFile": "${workspaceRoot}/.env", + "debugOptions": [ + "WaitOnAbnormalExit", + "WaitOnNormalExit" + ] + }, + { + "name": "External Terminal/Console", + "type": "python", + "request": "launch", + "stopOnEntry": true, + "pythonPath": "${config:python.pythonPath}", + "program": "${file}", + "cwd": "", + "console": "externalTerminal", + "env": {}, + "envFile": "${workspaceRoot}/.env", + "debugOptions": [ + "WaitOnAbnormalExit", + "WaitOnNormalExit" + ] + }, + { + "name": "Django", + "type": "python", + "request": "launch", + "stopOnEntry": true, + "pythonPath": "${config:python.pythonPath}", + "program": "${workspaceRoot}/manage.py", + "cwd": "${workspaceRoot}", + "args": [ + "runserver", + "--noreload", + "--nothreading" + ], + "env": {}, + "envFile": "${workspaceRoot}/.env", + "debugOptions": [ + "WaitOnAbnormalExit", + "WaitOnNormalExit", + "RedirectOutput", + "DjangoDebugging" + ] + }, + { + "name": "Flask", + "type": "python", + "request": "launch", + "stopOnEntry": false, + "pythonPath": "${config:python.pythonPath}", + "program": "fully qualified path fo 'flask' executable. Generally located along with python interpreter", + "cwd": "${workspaceRoot}", + "env": { + "FLASK_APP": "${workspaceRoot}/quickstart/app.py" + }, + "args": [ + "run", + "--no-debugger", + "--no-reload" + ], + "envFile": "${workspaceRoot}/.env", + "debugOptions": [ + "WaitOnAbnormalExit", + "WaitOnNormalExit", + "RedirectOutput" + ] + }, + { + "name": "Flask (old)", + "type": "python", + "request": "launch", + "stopOnEntry": false, + "pythonPath": "${config:python.pythonPath}", + "program": "${workspaceRoot}/run.py", + "cwd": "${workspaceRoot}", + "args": [], + "env": {}, + "envFile": "${workspaceRoot}/.env", + "debugOptions": [ + "WaitOnAbnormalExit", + "WaitOnNormalExit", + "RedirectOutput" + ] + }, + { + "name": "Pyramid", + "type": "python", + "request": "launch", + "stopOnEntry": true, + "pythonPath": "${config:python.pythonPath}", + "cwd": "${workspaceRoot}", + "env": {}, + "envFile": "${workspaceRoot}/.env", + "args": [ + "${workspaceRoot}/development.ini" + ], + "debugOptions": [ + "WaitOnAbnormalExit", + "WaitOnNormalExit", + "RedirectOutput", + "Pyramid" + ] + }, + { + "name": "Watson", + "type": "python", + "request": "launch", + "stopOnEntry": true, + "pythonPath": "${config:python.pythonPath}", + "program": "${workspaceRoot}/console.py", + "cwd": "${workspaceRoot}", + "args": [ + "dev", + "runserver", + "--noreload=True" + ], + "env": {}, + "envFile": "${workspaceRoot}/.env", + "debugOptions": [ + "WaitOnAbnormalExit", + "WaitOnNormalExit", + "RedirectOutput" + ] + }, + { + "name": "Attach (Remote Debug)", + "type": "python", + "request": "attach", + "localRoot": "${workspaceRoot}", + "remoteRoot": "${workspaceRoot}", + "port": 3000, + "secret": "my_secret", + "host": "localhost" + } + ] +} \ No newline at end of file diff --git a/exercises/exercise-001.py b/exercises/exercise-001.py new file mode 100644 index 00000000..8480000e --- /dev/null +++ b/exercises/exercise-001.py @@ -0,0 +1,13 @@ +# Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5, +# between 2000 and 3200 (both included). +# The numbers obtained should be printed in a comma-separated sequence on a single line. + +numbers = [] + +for i in range(2000, 3201): + if (i%5 != 0) and (i%7 == 0): + numbers.append(str(i)) + +result = ', '.join(numbers) + +print('The numbers divided by 7 but not divided by 0 are: \n' + result) diff --git a/exercises/exercise-002.py b/exercises/exercise-002.py new file mode 100644 index 00000000..1a52cc27 --- /dev/null +++ b/exercises/exercise-002.py @@ -0,0 +1,18 @@ +# Write a program which can compute the factorial of a given numbers. +# The results should be printed in a comma-separated sequence on a single line. +# Suppose the following input is supplied to the program: +# 8 +# Then, the output should be: +# 40320 + +def factorial(x): + if x == 0: + return 1 + return x * factorial(x-1) + + +number = int(input('Please, provide me a number: ')) + +message = factorial(number) + +print(message) diff --git a/exercises/exercise-003.py b/exercises/exercise-003.py new file mode 100644 index 00000000..7b21042a --- /dev/null +++ b/exercises/exercise-003.py @@ -0,0 +1,14 @@ +# With a given integral number n, write a program to generate a dictionary that contains (i, i*i) such that is an integral number between 1 and n (both included). and then the program should print the dictionary. +# Suppose the following input is supplied to the program: +# 8 +# Then, the output should be: +# {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64} + +dictionary = dict() + +number = int(input('Please provide me a number: ')) + +for i in range(1, number + 1): + dictionary[i] = i * i + +print(dictionary) \ No newline at end of file diff --git a/exercises/exercise-004.py b/exercises/exercise-004.py new file mode 100644 index 00000000..07d7481c --- /dev/null +++ b/exercises/exercise-004.py @@ -0,0 +1,18 @@ +# Write a program which accepts a sequence of comma-separated numbers from +# console and generate a list and a tuple which contains every number. +# Suppose the following input is supplied to the program: +# 34,67,55,33,12,98 +# Then, the output should be: +# ['34', '67', '55', '33', '12', '98'] +# ('34', '67', '55', '33', '12', '98') + +def trim(x): + return x.strip() + +string = input('Provide me numbers (sepparated by comma): ') + +numbers = string.split(',') + +numbers = list(map(trim, numbers)) + +print(numbers) \ No newline at end of file diff --git a/exercises/exercise-005.py b/exercises/exercise-005.py new file mode 100644 index 00000000..25b5e28c --- /dev/null +++ b/exercises/exercise-005.py @@ -0,0 +1,20 @@ +# Question: +# Define a class which has at least two methods: +# getString: to get a string from console input +# printString: to print the string in upper case. +# Also please include simple test function to test the class methods. + +class MyClass: + + def __init__(self): + self.s = '' + + def getString(self): + self.s = input('Please give me a string: ') + + def pringString(self): + print(self.s.upper()) + +myClass = MyClass() +myClass.getString() +myClass.pringString() \ No newline at end of file diff --git a/exercises/exercise-006.py b/exercises/exercise-006.py new file mode 100644 index 00000000..d826859e --- /dev/null +++ b/exercises/exercise-006.py @@ -0,0 +1,26 @@ +# Write a program that calculates and prints the value according to the given formula: +# Q = Square root of [(2 * C * D)/H] +# Following are the fixed values of C and H: +# C is 50. H is 30. +# D is the variable whose values should be input to your program in a comma-separated sequence. +# Example +# Let us assume the following comma separated input sequence is given to the program: +# 100,150,180 +# The output of the program should be: +# 18,22,24 +import math + +def formula(d): + if isinstance(d, str): d = int(d) + c = 50 + h = 30 + x = (2 * c * d) / h + return int(math.sqrt(x)) + +raw = input('Please give me numbers (sepparated by comma): ') + +strings = raw.split(',') + +numbers = list(map(formula, strings)) + +print(numbers) diff --git a/exercises/exercise-007.py b/exercises/exercise-007.py new file mode 100644 index 00000000..6d51a91b --- /dev/null +++ b/exercises/exercise-007.py @@ -0,0 +1,22 @@ +# Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. +# The element value in the i-th row and j-th column of the array should be i*j. +# Note: i=0,1.., X-1; j=0,1,¡­Y-1. +# Example +# Suppose the following inputs are given to the program: +# 3,5 +# Then, the output of the program should be: +# [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]] + +raw = input('Give me a number: ') +dimensions = [int(num) for num in raw.split(',')] +x = dimensions[0] +y = dimensions[1] +matrix = list() + +for i in range(x): + dimension = list() + for j in range(y): + dimension.append(i*j) + matrix.append(dimension) + +print(matrix) diff --git a/exercises/exercise-008.py b/exercises/exercise-008.py new file mode 100644 index 00000000..0888c89d --- /dev/null +++ b/exercises/exercise-008.py @@ -0,0 +1,16 @@ +# Write a program that accepts a comma separated sequence of words as input and prints the words in a comma-separated sequence after sorting them alphabetically. +# Suppose the following input is supplied to the program: +# without,hello,bag,world +# Then, the output should be: +# bag,hello,without,world + +def trim(x): + return x.strip() + +raw = input('Please provide me words: ') + +strings = list(map(trim, raw.split(','))) + +strings.sort() + +print(strings) \ No newline at end of file diff --git a/exercises/exercise-009.py b/exercises/exercise-009.py new file mode 100644 index 00000000..3e3c9d9e --- /dev/null +++ b/exercises/exercise-009.py @@ -0,0 +1,19 @@ +# Write a program that accepts sequence of lines as input and prints the lines after making all characters in the sentence capitalized. +# Suppose the following input is supplied to the program: +# Hello world +# Practice makes perfect +# Then, the output should be: +# HELLO WORLD +# PRACTICE MAKES PERFECT + +print('Please, write all you want. When you finish just leave a blank space before press enter') + +lines = list() +raw = input() + +while (raw.strip() != ''): + lines.append(raw.upper()) + raw = input() + +for line in lines: + print(line) diff --git a/exercises/exercise-010.py b/exercises/exercise-010.py new file mode 100644 index 00000000..75cbe544 --- /dev/null +++ b/exercises/exercise-010.py @@ -0,0 +1,16 @@ +# Write a program that accepts a sequence of whitespace separated words as input and +# prints the words after removing all duplicate words and sorting them alphanumerically. +# Suppose the following input is supplied to the program: +# hello world and practice makes perfect and hello world again +# Then, the output should be: +# again and hello makes perfect practice world + +raw = input() + +words = list(set(raw.split(' '))) + +words.sort() + +text = ' '.join(words) + +print(text) \ No newline at end of file diff --git a/exercises/exercise-011.py b/exercises/exercise-011.py new file mode 100644 index 00000000..672e4ce2 --- /dev/null +++ b/exercises/exercise-011.py @@ -0,0 +1,18 @@ +# Write a program which accepts a sequence of comma separated 4 digit binary numbers as its input and then check whether +# they are divisible by 5 or not. The numbers that are divisible by 5 are to be printed in a comma separated sequence. +# Example: +# 0100,0011,1010,1001 +# Then the output should be: +# 1010 +# Notes: Assume the data is input by console. + +raw = input('Give me binaries! ') + +divisibles = list() + +for binary in raw.split(','): + number = int(binary, 2) + if number%5 == 0: + divisibles.append(binary) + +print(divisibles) \ No newline at end of file diff --git a/exercises/exercise-012.py b/exercises/exercise-012.py new file mode 100644 index 00000000..aaed8f8f --- /dev/null +++ b/exercises/exercise-012.py @@ -0,0 +1,11 @@ +# Write a program, which will find all such numbers between 1000 and 3000 +# (both included) such that each digit of the number is an even number. +# The numbers obtained should be printed in a comma-separated sequence on a single line. + +numbers = list() + +for i in range(1000, 3001): + if i%2 != 0: + numbers.append(str(i)) + +print(','.join(numbers)) \ No newline at end of file From cefde01947aa64284fbb8e79a6d9ac7fb921969c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Encarnaci=C3=B3n=20Ortiz?= Date: Sun, 27 May 2018 01:46:36 -0400 Subject: [PATCH 2/4] - Updated README.md --- README | 0 README.md | 5 +++++ 2 files changed, 5 insertions(+) delete mode 100644 README create mode 100644 README.md diff --git a/README b/README deleted file mode 100644 index e69de29b..00000000 diff --git a/README.md b/README.md new file mode 100644 index 00000000..b461be27 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Python Challenging Programming Exercises Solved + +I will be adding the [Python-programming-exercises](https://github.com/zhiwehu/Python-programming-exercises) responses. Feel free to explore this project. + +Enyoi! \ No newline at end of file From ec62ae97323e5f3218ada7e1a6bbcb639447b9a4 Mon Sep 17 00:00:00 2001 From: Andres Encarnacion Date: Sun, 27 May 2018 01:48:05 -0400 Subject: [PATCH 3/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b461be27..2af4f37a 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,4 @@ I will be adding the [Python-programming-exercises](https://github.com/zhiwehu/Python-programming-exercises) responses. Feel free to explore this project. -Enyoi! \ No newline at end of file +Enyoy! From b9bee3eac58fc40a8b1eb4998ffef43302dae68a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Encarnaci=C3=B3n=20Ortiz?= Date: Thu, 30 Aug 2018 08:24:27 -0400 Subject: [PATCH 4/4] Completed more exerecises --- exercises/exercise-013.py | 21 +++++++++++++++++++++ exercises/exercise-014.py | 22 ++++++++++++++++++++++ exercises/exercise-015.py | 14 ++++++++++++++ exercises/exercise-016.py | 11 +++++++++++ exercises/exercise-017.py | 28 ++++++++++++++++++++++++++++ exercises/exercise-018.py | 14 ++++++++++++++ 6 files changed, 110 insertions(+) create mode 100644 exercises/exercise-013.py create mode 100644 exercises/exercise-014.py create mode 100644 exercises/exercise-015.py create mode 100644 exercises/exercise-016.py create mode 100644 exercises/exercise-017.py create mode 100644 exercises/exercise-018.py diff --git a/exercises/exercise-013.py b/exercises/exercise-013.py new file mode 100644 index 00000000..680bc028 --- /dev/null +++ b/exercises/exercise-013.py @@ -0,0 +1,21 @@ +# Write a program that accepts a sentence and calculate the number of letters and digits. +# Suppose the following input is supplied to the program: +# hello world! 123 +# Then, the output should be: +# LETTERS 10 +# DIGITS 3 + +raw = input('Write something: ') +stats = { 'digits': 0, 'letters': 0 } + +for char in raw: + if char.isdigit(): + stats['digits']+=1 + elif char.isalpha(): + stats['letters']+=1 + else: + pass + +print("LETTERS:", stats['letters']) +print("NUMBERS:", stats['digits']) + diff --git a/exercises/exercise-014.py b/exercises/exercise-014.py new file mode 100644 index 00000000..f970cd3e --- /dev/null +++ b/exercises/exercise-014.py @@ -0,0 +1,22 @@ +# Write a program that accepts a sentence and calculate the number of upper case letters and lower case letters. +# Suppose the following input is supplied to the program: +# Hello world! +# Then, the output should be: +# UPPER CASE 1 +# LOWER CASE 9 + +raw = input("Write something:") + +stats = { 'upper': 0, 'lower': 0 } + +for char in raw: + if char.isupper(): + stats['upper'] += 1 + elif char.islower(): + stats['lower'] += 1 + else: + pass + +print('UPPER: ', stats['upper']) +print('LOWER: ', stats['lower']) + diff --git a/exercises/exercise-015.py b/exercises/exercise-015.py new file mode 100644 index 00000000..642dfb1a --- /dev/null +++ b/exercises/exercise-015.py @@ -0,0 +1,14 @@ +# Write a program that computes the value of a+aa+aaa+aaaa with a given digit as the value of a. +# Suppose the following input is supplied to the program: +# 9 +# Then, the output should be: +# 11106 + +raw = input('Write a number: ') + +a = int('%s' % (raw)) +aa = int('%s%s' % (raw, raw)) +aaa = int('%s%s%s' % (raw, raw, raw)) +aaaa = int('%s%s%s%s' % (raw, raw, raw, raw)) + +print(a + aa + aaa + aaaa) \ No newline at end of file diff --git a/exercises/exercise-016.py b/exercises/exercise-016.py new file mode 100644 index 00000000..8f323f76 --- /dev/null +++ b/exercises/exercise-016.py @@ -0,0 +1,11 @@ +# Use a list comprehension to square each odd number in a list. The list is input by a sequence of comma-separated numbers. +# Suppose the following input is supplied to the program: +# 1, 2, 3, 4, 5, 6, 7, 8, 9 +# Then, the output should be: +# 1, 3, 5, 7, 9 + +raw = input('Write a serie of numbers: ') + +numbers = [i for i in raw.split(',') if int(i) % 2 != 0] + +print(','.join(numbers)) diff --git a/exercises/exercise-017.py b/exercises/exercise-017.py new file mode 100644 index 00000000..575b139b --- /dev/null +++ b/exercises/exercise-017.py @@ -0,0 +1,28 @@ +# Write a program that computes the net amount of a bank account based a transaction log from console input. The transaction log format is shown as following: +# D 100 +# W 200 +# D means deposit while W means withdrawal. +# Suppose the following input is supplied to the program: +# D 300 +# D 300 +# W 200 +# D 100 +# Then, the output should be: +# 500 + +amount = 0 +while True: + raw = input("Make a transaction: ") + if not raw: + break + args = raw.split(' ') + operation = args[0] + value = float(args[1]) + if operation is 'D': + amount += value + elif operation is 'W': + amount -= value + else: + pass + +print('Total amount:', amount) diff --git a/exercises/exercise-018.py b/exercises/exercise-018.py new file mode 100644 index 00000000..d2da5ed4 --- /dev/null +++ b/exercises/exercise-018.py @@ -0,0 +1,14 @@ +# A website requires the users to input username and password to register. Write a program to check the validity of password input by users. +# Following are the criteria for checking the password: +# 1. At least 1 letter between [a-z] +# 2. At least 1 number between [0-9] +# 1. At least 1 letter between [A-Z] +# 3. At least 1 character from [$#@] +# 4. Minimum length of transaction password: 6 +# 5. Maximum length of transaction password: 12 +# Your program should accept a sequence of comma separated passwords and will check them according to the above criteria. Passwords that match the criteria are to be printed, each separated by a comma. +# Example +# If the following passwords are given as input to the program: +# ABd1234@1,a F1#,2w3E*,2We3345 +# Then, the output of the program should be: +# ABd1234@1