Skip to content

Commit 78ea469

Browse files
Added all Python projects
1 parent a19c873 commit 78ea469

File tree

20 files changed

+100
-0
lines changed

20 files changed

+100
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Python Project: Rock-Paper-Scissors
2+
3+
## Description
4+
This project implements the classic game of Rock-Paper-Scissors, allowing a user to play against the computer. The game follows standard rules:
5+
- **Rock beats Scissors**
6+
- **Scissors beats Paper**
7+
- **Paper beats Rock**
8+
9+
The program uses Python's `random` module to generate the computer's choice and determines the winner based on predefined rules.
10+
11+
## Tools Used
12+
- **Python** : The programming language used to develop the project.
13+
- **Random Module** : To generate random choices for the computer.
14+
15+
## How It Works
16+
1. The user is prompted to enter their choice (Rock, Paper, or Scissors).
17+
2. The computer generates a random choice from the list: `['rock', 'paper', 'scissor']`.
18+
3. The program compares the user's choice with the computer's choice using the game rules to determine the winner.
19+
4. The result is displayed, and the user is asked whether they want to play again.
20+
21+
## Code Structure
22+
- **Choices Definition**: The valid options are stored in a list: `['rock', 'paper', 'scissor']`.
23+
- **Functions**:
24+
- `get_computer_choice()` : Returns a random choice for the computer.
25+
- `get_user_choice()` : Prompts the user for their choice and validates the input.
26+
- `determine_winner(user_choice, computer_choice)` : Determines the outcome based on game rules.
27+
- `game_play()` : Orchestrates the game, handles inputs, and loops until the user decides to stop.
28+
29+
## Prerequisites
30+
- Python 3.x installed on your machine.
31+
32+
## Installation and Usage
33+
1. Clone this repository :
34+
```bash
35+
git clone https://github.com/chatterjee007-dev/Data-Science-Portfolio.git
36+
37+
2. Navigate to the project directory :
38+
```bash
39+
cd Data-Science-Portfolio/Python/Project_01_Rock_Paper_Scissors
40+
41+
3. Run the script :
42+
```bash
43+
jupyter notebook Rock_Paper_Scissors.ipynb
44+
45+
## Sample Run
46+
Let's play!
47+
Enter your choice, Rock paper or scissor? rock
48+
You chose: rock
49+
Computer chose: scissor
50+
You win!
51+
Do you want to play again? (y/n): y
52+
Enter your choice, Rock paper or scissor? paper
53+
You chose: paper
54+
Computer chose: paper
55+
It's a tie!
56+
Do you want to play again? (y/n): n
57+
58+
## Future Enhancements
59+
60+
1. **Add More Game Modes** :
61+
- Introduce additional game modes like "Rock-Paper-Scissors-Lizard-Spock" for more variety.
62+
63+
2. **Improve User Interface** :
64+
- Create a graphical user interface (GUI) using libraries such as Tkinter or PyQt to make the game more interactive and user-friendly.
65+
66+
3. **Track Scores** :
67+
- Implement a feature that tracks the player's wins, losses, and ties over multiple rounds.
68+
69+
4. **Difficulty Levels** :
70+
- Add difficulty levels where the computer can make smarter choices based on previous player behavior.
71+
72+
5. **Leaderboard** :
73+
- Store high scores or game history in a text file or database to create a leaderboard feature.
74+
75+
6. **Multiplayer Option** :
76+
- Allow two players to play against each other on the same device or over the network.
77+
78+
79+
80+
81+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyNvNZkVif8ko7fMs4sK+SAt"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["# **PYTHON PROJECT : ROCK-PAPER-SCISSOR**\n","---\n","## Write a program that allows the user to play a game of Rock Paper Scissors against the computer. The program should prompt the user to enter their choice (Rock, Paper, or Scissors) and then randomly generate a choice for the computer. The program should then determine the winner based on the following rules:\n","\n","- ## Rock beats Scissors\n","- ## Scissors beats Paper\n","- ## Paper beats Rock\n","---\n","## **Importing module 'random' :**\n","\n","- ### 'random' is a python module that generates random variables (random integers, bytes, sequences) based on the input that we give.\n","- ### In Rock Paper Scissors, as we randomly choose any one of the three, so we need to import random for that.\n","---\n","## **Defining our choices :**\n","- ### Creating a list of our choices."],"metadata":{"id":"nzo973D7-QSa"}},{"cell_type":"code","execution_count":null,"metadata":{"id":"SkhVmold9E8W"},"outputs":[],"source":["import random\n","choices = ['rock','paper','scissor']"]},{"cell_type":"markdown","source":["### Now the logic to do this is : we have to enter our choice and the computer will randomly select one of these 3 choices > then we will define our rule, like - if 'scissor' > 'paper' > 'rock'.\n","## **Defining a function for computer's random choice :**\n","---\n","- ### **'random.choice'** - is a function present in module 'random' that randomly chooses one of the choices that we give from the list 'choices'.\n"],"metadata":{"id":"BKKy2tDBAemJ"}},{"cell_type":"code","source":["def get_computer_choice() :\n"," return random.choice(choices)"],"metadata":{"id":"mvWysUa9Aabd"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["## **Defining a user function :**\n","---\n","- ### Asking user to enter a choice and using **lower()** function convert the input strings into lower vase as our list 'choices' are all in lower case.\n","- ### Now if the user input present in the list 'choices', then the function will return the user choice."],"metadata":{"id":"z9voHCiyCkNG"}},{"cell_type":"code","source":["def get_user_choice() :\n"," user_choice = input(\"Enter your choice, Rock paper or scissor? \").lower()\n"," if user_choice in choices :\n"," return user_choice\n"," else :\n"," return \"Invalid choice. Please try again.\""],"metadata":{"id":"t6A9ZoxSCjgk"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["## **Applying game logic to determine the winner :**\n","---\n","- ### Defining function **'determine_winner( )'** below which is dependent on two parameters - user choice and computer choice.\n","- ### Now the variables '***user_choice***' and '***computer_choice***' are local variables which only apply for the function itself. So within the function we have to define what each of these variables stands for.\n","## **Let's define the logic within the function 'winner' :**\n","---\n","- ### If both user and computer choices are same - then it will be a tie.\n","- ### Then within **'elif'** statement we will defines all the instances where the user can win.\n","- ### Otherwise the computer wins."],"metadata":{"id":"TUq0eU6SE31h"}},{"cell_type":"code","source":["def determine_winner(user_choice, computer_choice) :\n","\n"," if user_choice == computer_choice :\n"," return \"It's a tie!\"\n","\n"," elif (user_choice == 'rock' and computer_choice == 'scissor') or \\\n"," (user_choice == 'scissor' and computer_choice == 'paper') or \\\n"," (user_choice == 'paper' and computer_choice == 'rock') :\n"," return \"You win!\"\n","\n"," else :\n"," return \"Computer wins!\""],"metadata":{"id":"qP-7M1NCEnAB"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["## **Defining our game function - 'game_play' :**\n","---\n","- ### Now **' while loop'** keeps running until the user manually stops the program.\n","- ### Defining local variables **'user_choice'** and **'computer_value'** which will call the two functions - **'get_user_choice()'** and **'get_computer_choice()'** which we have defined earlier and the output will be assigned to them."],"metadata":{"id":"ec4dSKXhQ08z"}},{"cell_type":"code","source":["def game_play() :\n"," print(\"Let's play!\")\n","\n"," while True :\n"," user_choice = get_user_choice()\n"," computer_value = get_computer_choice()\n"," print(f\"You chose : {user_choice}\") # It will print the user and computer choice\n"," print(f\"Computer chose : {computer_value}\")\n"," result = determine_winner(user_choice, computer_value) # Call function 'winner' to determine the winner\n"," print(result)\n","\n"," play_again = input(\"do you want to play again? (y/n) : \").lower()\n"," if play_again != \"y\" : # If the user don't want to play again, then the loop breaks\n"," break"],"metadata":{"id":"egjoQNBWQPsj"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["## Let's run the function :"],"metadata":{"id":"8OwK6hYk_t0L"}},{"cell_type":"code","source":["game_play()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"63SvIx_gqtRW","outputId":"877cb26d-727e-4605-fff8-e1ed25f121a7","executionInfo":{"status":"ok","timestamp":1727012550298,"user_tz":-330,"elapsed":14760,"user":{"displayName":"Anindya Chatterjee","userId":"07568261938821269796"}}},"execution_count":null,"outputs":[{"name":"stdout","output_type":"stream","text":["Let's play!\n","Enter your choice, Rock paper or scissor? rock\n","You chose : rock\n","Computer chose : scissor\n","You win!\n","do you want to play again? (y/n) : y\n","Enter your choice, Rock paper or scissor? paper\n","You chose : paper\n","Computer chose : paper\n","It's a tie!\n","do you want to play again? (y/n) : n\n"]}]}]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyOzHBhdWe6ZSjMeD4jLN56A"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["# **PYTHON PROJECT : COUNT FREQUENCY**\n","---\n","- ## Write a program that takes a string as input, and counts the frequency of each word in the string, there might be repeated characters in the string. Your task is to find the highest frequency and returns the length of the highest-frequency word.\n","\n","## **Example :**\n","\n","- ### Input string = write write write all the number from from from 1 to 100\n","- ### Output = Length of the highest-frequency word : 5\n","---\n","## Importing **'Regular expressions' / 're' :**\n","- ### **'re'** is a built-in Python module for working with regular expressions.\n","- ### Regular expressions are sequences of characters used to find and match patterns in strings.\n","---\n","### Below we are defining a function **'highest_freq_word()'** and giving the input string as parameter which needs to be processed :\n"],"metadata":{"id":"nI5aYsSvEXsf"}},{"cell_type":"code","execution_count":null,"metadata":{"id":"tweaiUfKD4GF"},"outputs":[],"source":["import re\n","\n","def highest_freq_word(string) :\n"," word_counts = {} # Defining an empty dictionary\n","\n"," for word in string.split(\" \") : # 'split()' will split the words in string on the basis of space and make a\n"," # list of them. Then we willpick every word using for loop\n"," if word in word_counts : # If the word in the list of strings present in the empty dictionary 'word_counts'\n"," # then like below line - the count will increase by 1.\n"," word_counts[word] += 1 # Here we are defining each word in the list as a key in our dictionary and the\n"," # value for it is the number of time the word appears in the string. So\n"," # everytime the word appears, the count will increase by 1.\n"," else :\n"," word_counts[word] = 1 # But if the word is not present in the dictionary, then we will initialize a key\n"," # with the value of 1.\n"," print(word_counts) # Print the dictionary.\n","\n"," # Defining the logic for highest frequency :\n"," highest = 0\n"," highest_word = ''\n","\n"," for word, count in word_counts.items() : # 'word_counts.items()' will provide pairs of keys and their\n"," # values from the dictionary 'word_counts' and for loop will\n"," # assign each key to 'word' and value to 'count' one by one.\n"," highest = count\n"," highest_word = word\n"," return highest_word, len(highest_word)"]},{"cell_type":"markdown","source":["### Below we have our main function (or the body of our logic) where we are calling all of the previously defined functions."],"metadata":{"id":"Kgpfq49_QJia"}},{"cell_type":"code","source":["if __name__ == \"__main__\" :\n"," string = input(\"Enter a string : \") # Getting user input\n"," high_frequency_word = highest_freq_word(string) # Calling the previously defined function on the string\n"," print(\"Length of highest frequency word : \", high_frequency_word)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"QV2h6KuFQPaR","executionInfo":{"status":"ok","timestamp":1727018795003,"user_tz":-330,"elapsed":48096,"user":{"displayName":"Anindya Chatterjee","userId":"07568261938821269796"}},"outputId":"79f63ec1-7b58-46c0-c4c3-9e3f1a1b4bc3"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Enter a string : write write write all the number from from from 1 to 100\n","{'write': 3, 'all': 1, 'the': 1, 'number': 1, 'from': 3, '1': 1, 'to': 1, '100': 1}\n","Length of highest frequency word : ('write', 5)\n"]}]},{"cell_type":"markdown","source":["## So in the output we can see 'write' is the word appeared most frequently and it's length is 5.\n","- ### It's not printing the word with highest length, but if the frequency of two words are same - then it will print the word with longer length."],"metadata":{"id":"3fTDncDqWEHm"}}]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyO3sPcRFnBMT2eMGg1+53O/"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["# **PYTHON PROJECT : CREATE MATHEMATICAL TABLE OF ANY NUMBER**\n","---\n","- ## Write a program that takes an integer as input, and creates a mathematical table of that number.\n","- ## Example :\n","### 1 x number = result\n","### 2 x number = result\n","### 3x number = result\n","### .\n","### .\n","### .\n","### 10 x number = result\n","---\n","## Defining a function 'multiplication()' which will take an input 'num'.\n","- ### Here the logic is - we want to multiply the parameter 'num' by each number from 1 to 10."],"metadata":{"id":"HSIppViTOtwy"}},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"10WLo95GOaWH","executionInfo":{"status":"ok","timestamp":1727034801393,"user_tz":-330,"elapsed":5335,"user":{"displayName":"Anindya Chatterjee","userId":"07568261938821269796"}},"outputId":"b303d480-ee3d-44ca-d7e1-71e49c5d7757"},"outputs":[{"output_type":"stream","name":"stdout","text":["Enter a number : 49\n","1 x 49 = 49\n","2 x 49 = 98\n","3 x 49 = 147\n","4 x 49 = 196\n","5 x 49 = 245\n","6 x 49 = 294\n","7 x 49 = 343\n","8 x 49 = 392\n","9 x 49 = 441\n","10 x 49 = 490\n"]}],"source":["def multiplication(num) :\n"," for i in range(1,11) : # Assigning numbers one by one to 'i' from 1 to 10 using for loop\n"," result = i*num\n"," print(i,'x',num,'=',result) # multiplying each number from 1 to 10 with the input number and print the result\n","\n","num = int(input(\"Enter a number : \")) # Taking input from user\n","multiplication(num) # Calling the function we have defined"]},{"cell_type":"markdown","source":["### So in the output we get multiplication table of 49 all the way upto 490."],"metadata":{"id":"XbLhkYdlSZjN"}}]}

Project_03_Math_Table/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyNie3Wo68j6wXvgux5qGHfp"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["# **PYTHON PROJECT : FINDING VALID STRING**\n","---\n","- ## Write a program that takes a string as input, and determines if all characters of the string appear the same number of times. If so, return \"YES\". Otherwise, check if removing just one character at any index in the string will make all characters appear the same number of times. If so, return \"YES\". Otherwise, return \"NO\".\n","## **Example :**\n","### **Input 1** = “abc”. This is a valid string because frequencies are {“a”: 1, “b”: 1, “c”: 1}\n","### **Output 1** - YES\n","### **Input 2** = “abcc”. This string is not valid as we can remove only 1 occurrence of “c”. That leaves character frequencies of { “a”: 1, “b”: 1 , “c”: 2 }\n","### **Output 2** – NO\n","---\n","## **Importing module 'Counter' :**\n","- ### As we need to count the frequency of each character, we will import the module 'Counter'.\n","- ### **Counter(string)** - It will count the frequency of each character in the input\n"," and create a dictionary where characters of input string will be keys and their frequency will be values.\n","---\n","- ### **set(count)** - will create set of all unique counts. So it checks how many values in 'count' are unique. If all the characters in our string appears same number of times - then the number of unique values will only be 1.\n","---\n","## **Below we will define 2 conditions using 'if' statement :**\n","- ### 1. If the number of unique values are all 1, then we will return \"YES\".\n","- ### 2. If we remove one character at any index in the string, will that make it a valid string ?\n"],"metadata":{"id":"p5ZFir9tUU7p"}},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"UgtcJWYUUTws","executionInfo":{"status":"ok","timestamp":1727038885672,"user_tz":-330,"elapsed":7771,"user":{"displayName":"Anindya Chatterjee","userId":"07568261938821269796"}},"outputId":"8a9814ed-00b5-4703-b0b5-941ce98fe5fd"},"outputs":[{"output_type":"stream","name":"stdout","text":["Enter a string : anindya\n","Characters and their frequency : Counter({'a': 2, 'n': 2, 'i': 1, 'd': 1, 'y': 1})\n","List of frequencies of all the characters : [2, 2, 1, 1, 1]\n","Is it a valid string? - NO\n"]}],"source":["from collections import Counter\n","\n","def check_valid(string) : # The input will be parameter 'string' of the function we have just defined\n"," char_count = Counter(string) # Counting the number of characters in the input string\n"," print(\"Characters and their frequency :\",char_count) # Printing the dictionary\n"," count = list(char_count.values()) # Making a list of all the values(frequencies) in dictionary 'char_count'\n"," print(\"List of frequencies of all the characters :\",count) # Printing the list\n"," if len(set(count)) == 1 : # It will check wheather there is only 1 unique count in the set > which means all\n"," return \"YES\" # characters appear same number of times.\n","\n"," # Implementing the second condition :\n","\n"," for i in range(len(count)) : # Assigning one by one value to 'i' from list of values 'count' using for loop.\n"," new_count = count[:i] + count[i+1:] # Initializing the value called 'new_count' which includes all values\n"," # before 'i'(count[:i]) and after 'i' which is 'i+1'(count[i+1:]).\n"," # So here 'i' is not included which we have picked using for loop.\n"," if len(set(new_count)) == 1 :\n"," return \"YES\"\n","\n"," return \"NO\"\n","\n","words = input(\"Enter a string : \") # Taking input from user\n","result = check_valid(words) # Calling the function\n","print(\"Is it a valid string? -\",result)"]},{"cell_type":"markdown","source":["### So it will return \"NO\" - because the characters 'a' and 'n' appear twice in my name."],"metadata":{"id":"5TAAsbQ6g5cf"}}]}

Project_04_Valid_String/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)