diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..05aaa795 --- /dev/null +++ b/.gitignore @@ -0,0 +1,140 @@ +### Python template +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*,cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py + +# Flask instance folder +instance/ + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# IPython Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# dotenv +.env + +# virtualenv +venv/ +ENV/ + +# Spyder project settings +.spyderproject + +# Rope project settings +.ropeproject +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/workspace.xml +.idea/tasks.xml +.idea/dictionaries +.idea/vcs.xml +.idea/jsLibraryMappings.xml + +# Sensitive or high-churn files: +.idea/dataSources.ids +.idea/dataSources.xml +.idea/dataSources.local.xml +.idea/sqlDataSources.xml +.idea/dynamic.xml +.idea/uiDesigner.xml + +# Gradle: +.idea/gradle.xml +.idea/libraries + +# Mongo Explorer plugin: +.idea/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Created by .ignore support plugin (hsz.mobi) + +# Sublime +*.sublime-project +*.sublime-workspace diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.md similarity index 100% rename from 100+ Python challenging programming exercises.txt rename to 100+ Python challenging programming exercises.md diff --git a/README.md b/README.md new file mode 100644 index 00000000..fa8b5a1c --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +# 0 - 100 Real Quick (Python Problems) + +Today, 2015 August 21st, I am going to embark on a journey of completing 100 python problems. Ranging from increasing complexities from 1-3. + +### Complexity Scale: + +Easy | Medium | Hard +--- | --- | --- +1 | 2 | 3 +
+>“I know it seems hard sometimes but remember one thing. +>Through every dark night, there's a bright day after that. +>So no matter how hard it get, stick your chest out, +>keep ya head up.... and handle it.” + +― Tupac Shakur + +
+ +```python +python = 'is going to be the language of choice' + +def anti_vowel(text): + text = text.lower() + vowels = ['a','e','i','o','u'] + return ''.join([x for x in text if x not in vowels]) + +anti_vowel(python) +Output: 's gng t b th lngg f chc' +``` + +You are welcome to work on these with me and send request pull your solutions! I promise I won't look at your solutions haha. Only for cross-checking once I complete my solution. Perhaps this repository might be of use to other learners who can see multiple solutions to a problem. + +In any case... +# Here... We... GO! :facepunch: diff --git a/Solutions.sublime-project b/Solutions.sublime-project new file mode 100644 index 00000000..2c63c085 --- /dev/null +++ b/Solutions.sublime-project @@ -0,0 +1,2 @@ +{ +} diff --git a/cse231/anagrams/anagrams.py b/cse231/anagrams/anagrams.py new file mode 100644 index 00000000..ba7b840e --- /dev/null +++ b/cse231/anagrams/anagrams.py @@ -0,0 +1,38 @@ +""" +anagrams.py + +Author: Rafeh Qazi + +Modified: June 2016 + +Copyright (C) 2016 Rafeh Qazi +""" + +from collections import Counter + + +def read_file(file): + with open(file, 'r') as f: + return [word[:-1] for word in f] + + +file_data = read_file('shortWordList.txt') + +# print(file_data) +# +# for _ in range(5): +# print('\n') +# +# anagrams = [] +# for word_1 in file_data: +# file_data.pop(0) +# word_1_anagrams = [] +# for word_2 in file_data: +# if Counter(word_1) == Counter(word_2): +# if word_2 not in anagrams: +# word_1_anagrams.append(word_2) +# anagrams.append(word_2) +# if word_1_anagrams: +# print(word_1_anagrams) + +# print(anagrams) diff --git a/cse231/anagrams/shortWordList.txt b/cse231/anagrams/shortWordList.txt new file mode 100644 index 00000000..110db744 --- /dev/null +++ b/cse231/anagrams/shortWordList.txt @@ -0,0 +1,19 @@ +able +acre +bale +beyond +binary +boat +brainy +care +cat +cater +crate +lawn +list +race +react +sheet +silt +slit +trace diff --git a/cse231/number_guessing_game.py b/cse231/number_guessing_game.py new file mode 100644 index 00000000..e6c9bc2f --- /dev/null +++ b/cse231/number_guessing_game.py @@ -0,0 +1,197 @@ +""" +number_guessing_game.py + +Author: Rafeh Qazi + +Modified: June 2016 + +Copyright (C) 2016 Rafeh Qazi +""" + + +def is_good_number(number): + """ + Let's a user know whether a number is properly formed. + Each number should be 5 digits long and should not repeat numbers. + + :param number: str + :return: boolean + + >>> is_good_number('12345') + True + >>> is_good_number('55432') + False + >>> is_good_number('444') + False + """ + try: + int(number) + except ValueError: + error_message() + + if not int(number): + return False + + elif len(number) != 5: + return False + + for num in number: + if number.count(num) > 1: + return False + + return True + + +def error_message(): + """ + Print an error message based on an ill formed number. + """ + print('The number must be 5 digits long and should not repeat numbers.') + print('Please fix your number!\n\n') + return get_number() + + +def blank_lines(): + """ + Print blank lines on a successfully formed number. + """ + print('--------------------------------------') + for _ in range(20): + print() + + +def get_number(): + """ + Ask the user for their number and retrieve that number. + + :return: str + """ + user_input = input('Please enter your 5 digit number here:\n\n') + if is_good_number(user_input): + return user_input + return error_message() + + +def report_results(user_number, secret_number): + """ + Report how many digits are correct. + Report how many digits are in correct position. + + :param user_number: str + :param secret_number: str + :return: tuple + + >>> report_results('54321', '12345') + (5, 1) + >>> report_results('25314', '23514') + (5, 3) + """ + correct_digits = 0 + correct_pos_digits = 0 + + for digit in user_number: + if digit in secret_number: + correct_digits += 1 + + for i in range(len(user_number)): + if user_number[i] == secret_number[i]: + correct_pos_digits += 1 + + return correct_digits, correct_pos_digits + + + +def player_progress(num_of_guesses, user_guess, correct_digits, correct_pos_digits): + """ + Report the player's current progress. + + Nums of guesses, their guess, num of correct digits, + and num of digits in the correct position. + + :param num_of_guesses: int + :param user_guess: str + :param correct_digits: int + :param correct_pos_digits: int + + >>> player_progress(5, '12345', 4, 3) + ------------------------------ + Used guesses: 5 + User guess: 12345 + Digits correct: 4 + Digits in correct position: 3 + ------------------------------ + """ + + print('------------------------------') + print('Used guesses: {}'.format(num_of_guesses)) + print('User guess: {}'.format(user_guess)) + print('Digits correct: {}'.format(correct_digits)) + print('Digits in correct position: {}'.format(correct_pos_digits)) + print('------------------------------') + + +def success_message(num_of_guesses): + """ + If user guesses the secret number successfully, + show how many guesses were used. + + :param num_of_guesses: int + + >>> success_message(5) + Congratulations! You have successfully guessed the secret number! + It took you 5 guesses to get it right! + """ + + print('Congratulations! You have successfully guessed the secret number!') + print('It took you {} guesses to get it right!'.format(num_of_guesses)) + + +def guesses_exceeded(allowed_guesses, num_of_guesses): + """ + Set the maximum number of guesses and notify the user + that they lost when they exceed the limit. + + :param allowed_guesses: int + :param num_of_guesses: int + + >>> guesses_exceeded(5, 6) + Allowed guesses: 5 + Used guesses: 6 + You have exceeded your limit of guesses and you still did not get it correct, you lose! + """ + + print('Allowed guesses: {}'.format(allowed_guesses)) + print('Used guesses: {}'.format(num_of_guesses)) + print('You have exceeded your limit of guesses and you still did not get it correct, you lose!') + + +def win(user_number, secret_number): + return report_results(user_number, secret_number)[1] == len(secret_number) + + +def main(): + """ + Number guessing game logic + """ + max_guesses = int(input('Enter the max number of allowed guesses: ')) + secret_number = get_number() + num_of_guesses = 0 + blank_lines() + while (input('Press Q or q to quit, anything else to continue playing: ').lower()) != 'q': + guess = get_number() + num_of_guesses += 1 + correct_digits, correct_pos_digits = report_results(guess, secret_number) + player_progress(num_of_guesses, guess, correct_digits, correct_pos_digits) + + if win(guess, secret_number): + return success_message(num_of_guesses) + + if num_of_guesses > max_guesses: + return guesses_exceeded(max_guesses, num_of_guesses) + + +if __name__ == '__main__': + import doctest + + doctest.testmod() + main() diff --git a/markdown-here.wiki b/markdown-here.wiki new file mode 160000 index 00000000..79a523f3 --- /dev/null +++ b/markdown-here.wiki @@ -0,0 +1 @@ +Subproject commit 79a523f39c17215dfe477c8e6126f432f1fd3aff diff --git a/my_solutions/Solution2.py b/my_solutions/Solution2.py new file mode 100644 index 00000000..c79ce8c8 --- /dev/null +++ b/my_solutions/Solution2.py @@ -0,0 +1,47 @@ +''' +Working I/O Template + + +Problem Statement +This is an introductory challenge. The purpose of this challenge is to give you a working I/O template in your preferred language. It includes scanning two integers from STDIN, calling a function, +returning a value, and printing it to STDOUT. + +The task is to scan two numbers from STDIN, and print the sum A+B on STDOUT. The code has already been provided for most of the popular languages. This is primarily for you to read +and inspect how the IO is handled. + + +Note: The code has been saved in a template, which you can submit if you want. Or, you may try rewriting it and building it up from scratch. + +Input Format +(This section specifies the Input Format.) +Given A and B on two different lines. + +Output Format +(This section specifies the Output Format.) +An integer that denotes Sum (A+B) +Constraints +(This section tells what input you can expect. You can freely assume that the input will remain within the boundaries specified. +As an example here given below, A and B will never be below 1 or above 1000.) +1≤A,B≤1000 +Sample Input + +2 +3 + + +Sample Output + +5 +The above sample should be taken seriously. The input will be 2 and 3 in two separate lines, and the output should be just one number, 5. You should not print any whitespace at the beginning of output +(e.g. " 5" or "\n5"), unless specifically asked for. Also, printing any extra non-whitespace characters such as "The answer is: 5" will result in a Wrong Answer, as the judging is done using diff checker. +''' + +# STDIN means read input. + +def solveMeFirst(a,b): + return (a+b) + +user1 = int(raw_input("Put your number here: ")) +user2 = int(raw_input("Put your number here: ")) + +print(solveMeFirst(user1, user2)) diff --git a/my_solutions/Solution3problem.py b/my_solutions/Solution3problem.py new file mode 100644 index 00000000..bfe49cf7 --- /dev/null +++ b/my_solutions/Solution3problem.py @@ -0,0 +1,3 @@ +Solution3problem.py + +'hi' \ No newline at end of file diff --git a/my_solutions/Solutions.sublime-project b/my_solutions/Solutions.sublime-project new file mode 100644 index 00000000..2c63c085 --- /dev/null +++ b/my_solutions/Solutions.sublime-project @@ -0,0 +1,2 @@ +{ +} diff --git a/my_solutions/Sublime Shortcus.md b/my_solutions/Sublime Shortcus.md new file mode 100644 index 00000000..dd1de4d3 --- /dev/null +++ b/my_solutions/Sublime Shortcus.md @@ -0,0 +1,5 @@ +#SHORTCUTS +ctrl+,, s Selection +ctrl+,, f File +ctrl+,, l Lines +ctrl+,, b Block diff --git a/my_solutions/__pycache__/Solution2.cpython-34.pyc b/my_solutions/__pycache__/Solution2.cpython-34.pyc new file mode 100644 index 00000000..02b044ed Binary files /dev/null and b/my_solutions/__pycache__/Solution2.cpython-34.pyc differ diff --git a/my_solutions/empty.sublime-workspace b/my_solutions/empty.sublime-workspace new file mode 100644 index 00000000..f0598317 --- /dev/null +++ b/my_solutions/empty.sublime-workspace @@ -0,0 +1,176 @@ +{ + "auto_complete": + { + "selected_items": + [ + ] + }, + "buffers": + [ + ], + "build_system": "", + "build_system_choices": + [ + ], + "build_varint": "", + "command_palette": + { + "height": 0.0, + "last_filter": "", + "selected_items": + [ + ], + "width": 0.0 + }, + "console": + { + "height": 0.0, + "history": + [ + ] + }, + "distraction_free": + { + "menu_visible": true, + "show_minimap": false, + "show_open_files": false, + "show_tabs": false, + "side_bar_visible": false, + "status_bar_visible": false + }, + "file_history": + [ + ], + "find": + { + "height": 23.0 + }, + "find_in_files": + { + "height": 0.0, + "where_history": + [ + ] + }, + "find_state": + { + "case_sensitive": false, + "find_history": + [ + "python", + "github", + "git", + "users", + "chesstastic", + "sta", + "git", + "rafeh" + ], + "highlight": true, + "in_selection": false, + "preserve_case": false, + "regex": false, + "replace_history": + [ + ], + "reverse": false, + "show_context": true, + "use_buffer2": true, + "whole_word": false, + "wrap": true + }, + "groups": + [ + { + "sheets": + [ + ] + } + ], + "incremental_find": + { + "height": 23.0 + }, + "input": + { + "height": 0.0 + }, + "layout": + { + "cells": + [ + [ + 0, + 0, + 1, + 1 + ] + ], + "cols": + [ + 0.0, + 1.0 + ], + "rows": + [ + 0.0, + 1.0 + ] + }, + "menu_visible": true, + "output.find_results": + { + "height": 0.0 + }, + "pinned_build_system": "", + "project": "empty.sublime-project", + "replace": + { + "height": 42.0 + }, + "save_all_on_build": true, + "select_file": + { + "height": 0.0, + "last_filter": "", + "selected_items": + [ + ], + "width": 0.0 + }, + "select_project": + { + "height": 500.0, + "last_filter": "", + "selected_items": + [ + [ + "", + "~/Dropbox/github/SublimeProjects/GitHub.sublime-project" + ] + ], + "width": 380.0 + }, + "select_symbol": + { + "height": 0.0, + "last_filter": "", + "selected_items": + [ + ], + "width": 0.0 + }, + "selected_group": 0, + "settings": + { + }, + "show_minimap": true, + "show_open_files": false, + "show_tabs": true, + "side_bar_visible": true, + "side_bar_width": 120.0, + "status_bar_visible": true, + "template_settings": + { + } +} diff --git a/README b/my_solutions/empty.txt similarity index 100% rename from README rename to my_solutions/empty.txt diff --git a/my_solutions/ex3io.py b/my_solutions/ex3io.py new file mode 100644 index 00000000..c40ad312 --- /dev/null +++ b/my_solutions/ex3io.py @@ -0,0 +1,46 @@ +ex3io.py + +Problem Statement + +You learnt about STDIN and STDOUT in Solve me first. + +This is the second challenge in the introduction series. The purpose of this challenge is to give you a working I/O template in your preferred language. It includes scanning two space-separated integers from STDIN in a loop over T lines, calling a function, returning a value, and printing it to STDOUT. + +A pseudo code looks like the following: + +read T +loop from 1 to T + read A and B + compute the sum + print value in a newline +end loop +The task is to scan two numbers from STDIN, and print the sum A+B on STDOUT. The code has already been provided for most of the popular languages. This is primarily for you to read and inspect how the IO is handled. + +Note: The code has been saved in a template, which you can submit if you want. Or, you may try rewriting it and building it up from scratch. + +Input Format +(This section specifies the Input Format.) +The first line contains T (number of test cases) followed by T lines +Each line contains A and B, separated by a space. + +As you can see that we have provided in advance the number of lines, we discourage the use of scanning till EOF as not every language has an easy way to handle that. In fact, every HackerRank challenge is designed in such a way that multitests begin with a T line to indicate the number of lines. + +Output Format +(This section specifies the Output Format.) +An integer that denotes Sum (A+B) printed on new line for every testcase. + +Constraints +(This section tells what input you can expect. You can freely assume that the input will remain within the boundaries specified.) +1≤T,A,B≤1000 + +Sample Input + +2 +2 3 +3 7 +Sample Output + +5 +10 +The above sample should be taken seriously. 2 in the first line describes how many lines will follow, and your test cases are 2, 3 and 3, 7 in two separate lines. Your output should be 5 and 10 printed on two separate lines. If you print extra lines or "The answer is: 5", any such extra characters in output will result in a Wrong Answer, as the judging is done using diff checker. + diff --git a/my_solutions/ex4arraymaps.py b/my_solutions/ex4arraymaps.py new file mode 100644 index 00000000..7575d4ab --- /dev/null +++ b/my_solutions/ex4arraymaps.py @@ -0,0 +1,16 @@ +''' + +ex4arraymapping.py + +Turn single-spaced numbers from strings to arrays of integers and then compute the sum. + +''' + +# [in] 6 +# [in] 1 2 3 4 5 6 +# [out] 21 + +n = input() +arr = map(int,raw_input().split()) #map integers to the strings. +print arr +print sum(arr) diff --git a/my_solutions/exercise1; List Comprehensions.ipynb.py b/my_solutions/exercise1; List Comprehensions.ipynb.py new file mode 100644 index 00000000..ad4baae4 --- /dev/null +++ b/my_solutions/exercise1; List Comprehensions.ipynb.py @@ -0,0 +1,13 @@ +# coding: utf-8 + + +''' +1st attempt at solution +for i in range(2000,3200): + if i % 7 == 0: + if i % 5 != 0: + print(i) +''' + +print ([i for i in range(2000,3200) if i % 7 == 0 and i % 5 != 0]) +