Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit 6f6c27b

Browse files
authored
Merge branch 'master' into New_Awesome-Python-Scripts
2 parents eb4278b + fbd51da commit 6f6c27b

File tree

143 files changed

+5573
-154
lines changed

Some content is hidden

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

143 files changed

+5573
-154
lines changed

Basic-Scripts/Compress_the_string.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from itertools import groupby
2+
uncompressed = str(input("Enter string to compress: ")) # user input for string
3+
print(*[(len(list(value)), str(key)) for key, value in groupby(uncompressed)]) # logic to print occurence of values in string
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
### Geocoding and Reverse Geocoding
2+
3+
#### This script takes an address and return its latitude and longitude.This process is called geocoding
4+
#### It can also perform the opposite operation that is take latitude, longitude and provide address.
5+
6+
#### I have used the LocationIQ website's geocoding api inorder to solve this problem.
7+
8+
#### In order to run this script you need to have a private token which is just a key for the api..
9+
10+
#### To obtain your own private token create a *free account* at https://locationiq.com/ and replace my private token with yours.
11+
12+
#### Remember, *don't share* your private token with anyone if you use your personal email for account creation.
13+
14+
#### Install the dependencies by using:
15+
16+
pip install -r requirements.txt
17+
18+
#### An Example of the script in action:
19+
20+
<img src="Sample.PNG" alt="Sample">
21+
22+
Loading
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import requests
2+
import sys
3+
4+
# If you want your own private_token please refer the README file for this script.
5+
private_token = "<Your Private Token>"
6+
7+
while True:
8+
choice = input(
9+
"Type G for geocoding , type R for reverse geocoding and type Q to quit the script:")
10+
print()
11+
12+
# If the user choose to perform Geocoding
13+
if choice.upper() == 'G':
14+
# Base Url for geocoding
15+
url = "https://us1.locationiq.com/v1/search.php"
16+
17+
address = input("Input the address: ")
18+
19+
# Parameters for the geocoding url
20+
data = {
21+
'key': private_token,
22+
'q': address,
23+
'format': 'json'
24+
}
25+
26+
response = requests.get(url, params=data)
27+
28+
# To run only if we get success response
29+
if response.status_code == 200:
30+
latitude = response.json()[0]['lat']
31+
longitude = response.json()[0]['lon']
32+
33+
print(f"The latitude of the given address is: {latitude}")
34+
print(f"The longitude of the given address is: {longitude}")
35+
print()
36+
else:
37+
sys.exit("Cant find what you where looking for")
38+
39+
# If the user choose to perform Reverse Geocoding
40+
elif choice.upper() == 'R':
41+
# Base Url for reverse geocoding
42+
url = "https://us1.locationiq.com/v1/reverse.php"
43+
44+
latitude_reverse = input("Enter the latitude: ")
45+
longitude_reverse = input("Enter the longitude: ")
46+
47+
# Parameters for the reverse geocoding url
48+
data = {
49+
'key': private_token,
50+
'lat': latitude_reverse,
51+
'lon': longitude_reverse,
52+
'format': 'json'
53+
}
54+
55+
response = requests.get(url, params=data)
56+
57+
# To run only if we get success response
58+
if response.status_code == 200:
59+
address = response.json()['display_name']
60+
print(f"The address is: {address}")
61+
print()
62+
else:
63+
print("Cant find what you where looking for.")
64+
65+
# If the user choose to Quit the program
66+
elif choice.upper() == 'Q':
67+
sys.exit("Thanks for using the script")
68+
69+
# To make sure only valid input is provided
70+
else:
71+
print("Please make a valid choice")
72+
print()
73+
74+
#Sample Input - Output:
75+
76+
#If you choose Geocoding:
77+
78+
#Address(Input): Rashtrapati Bhavan
79+
#Latitude(Output): 28.614458
80+
#Longitude(Output): 77.199594
81+
82+
#If you choose Reverse-Geocoding:
83+
84+
#Latitude(Input): 28.614458
85+
#Longitude(Input): 77.199594
86+
#Address(Output): Rashtrapati Bhavan, Rajpath, Presidential Estate, Chanakya Puri Tehsil, New Delhi, Delhi, 110004, India
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
certifi==2020.6.20
2+
chardet==3.0.4
3+
idna==2.10
4+
requests==2.24.0
5+
urllib3==1.25.10
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# function to convert infix to postfix using array as stack
2+
def infix_to_postfix(infix_expression):
3+
"""
4+
Function to change infix expression to postfix one
5+
Params:
6+
infix_expression: Infix Expression provided by user
7+
Returns:
8+
postfix_expression: Postfix Expression convertef from Infix one
9+
"""
10+
11+
# initially empty stack
12+
stack = []
13+
# initially empty postfix_expression
14+
postfix_expression = ""
15+
for char in infix_expression:
16+
# if an operand then put it directly in postfix expression
17+
if char not in operators:
18+
postfix_expression += char
19+
# else if operators should be put in stack
20+
elif char == "(":
21+
"""append function to push
22+
elements in the stack"""
23+
stack.append("(")
24+
elif char == ")":
25+
while stack and stack[-1] != "(":
26+
postfix_expression += stack.pop()
27+
""" pop function to pop
28+
elements from stack in LIFO order """
29+
stack.pop()
30+
else:
31+
"""if priority of char in infix_expression is less than or equal to
32+
char at stack[-1] pop out and put in postfix_expression"""
33+
while (
34+
stack and stack[-1] != "(" and priorities[char] <= priorities[stack[-1]]
35+
):
36+
postfix_expression += stack.pop()
37+
stack.append(char)
38+
while stack:
39+
postfix_expression += stack.pop()
40+
return postfix_expression
41+
42+
43+
# Set of operators
44+
operators = set(["+", "-", "*", "/", "(", ")", "^"])
45+
46+
# dictionary having priorities
47+
priorities = {"+": 1, "-": 1, "*": 2, "/": 2, "^": 3}
48+
49+
print("Input")
50+
infix_expression = input("Enter infix expression\n")
51+
print("Output")
52+
53+
# Displaying the Output
54+
print("Postfix expression: ", infix_to_postfix(infix_expression))
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
### Infix to Postfix
2+
3+
> • In Infix expression operator is in between every pair of operands.<br>
4+
> • In Postfix expression, the operator is followed for every pair of operands.
5+
6+
> Infix expression is converted to postfix conversion using Stack.
7+
> Postfix expression is evaluated using Stack in Left to Right order.
8+
9+
##### If the scanned character is operand, show it as output. Else, If precedence of scanned operator is greater than the precedence of the operator in the stack,push it.
10+
11+
> Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the scanned operator.
12+
13+
• If the scanned input is '(', push it into stack<br>
14+
• If the scanned input is ')' ,pop the stack and the output it until '(' comes.<br>
15+
• Repeat above steps. Continue Pop and output from stack until it becomes empty.
16+
17+
##### It makes the code more efficient and even reduces the time complexity.
18+
### Constraints
19+
"""
20+
Input:
21+
Enter infix expression: A+C*(B^D-E)^(G+H*K)-K
22+
Output:
23+
Postfix expression: ACBD^E-GHK*+^*+K-
24+
25+
Time Complexity : O(n)
26+
Space Complexity: Θ(n)
27+
"""

Basic-Scripts/README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
# Basic-Scripts
2-
Basic-Scripts is a collection of basic python scripts for newbies in python to understand basic concept of python programming.
3-
- Data Struture in python
4-
- Conditional
5-
- Loop
6-
- Exception Handling
7-
- Algorithms
8-
- OOP
9-
- Class
10-
- Object
1+
#Basic-Scripts
2+
Basic-Scripts is a collection of basic python scripts for newbies in python to understand basic concept of python programming.
3+
-Data Structure in python
4+
-Conditional
5+
-Loop
6+
-Exception handling
7+
-Algorithms
8+
-OOP
9+
-Class
10+
-Object
11+
12+
13+

Basic-Scripts/Rock_paper_scissor.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Mon Aug 17 14:24:23 2020
4+
5+
@author: Gulshan
6+
"""
7+
# Function rock_paper_scissor--->
8+
def rock_paper_scissor(num1,num2,bit1,bit2):
9+
play_pos1=int(num1[bit1])%3 #Play Position For Player 1
10+
play_pos2=int(num2[bit2])%3 #Play Position For Player 2
11+
if(player_one[play_pos1]==player_two[play_pos2]):
12+
print("Draw")
13+
elif(player_one[play_pos1]=="Rock" and player_two[play_pos2]=="Scissor"):
14+
print("Player one wins!!")
15+
elif(player_one[play_pos1]=="Rock" and player_two[play_pos2]=="Paper"):
16+
print("Player two wins!!")
17+
elif(player_one[play_pos1]=="Paper" and player_two[play_pos2]=="Scissor"):
18+
print("Player two wins!!")
19+
elif(player_one[play_pos1]=="Paper" and player_two[play_pos2]=="Rock"):
20+
print("Player one wins!!")
21+
elif(player_one[play_pos1]=="Scissor" and player_two[play_pos2]=="Rock"):
22+
print("Player two wins!!")
23+
elif(player_one[play_pos1]=="Scissor" and player_two[play_pos2]=="Paper"):
24+
print("Player one wins!!")
25+
26+
# Main Function To Execute Rock Paper Scissor
27+
if __name__ == '__main__':
28+
player_one={0:'Rock',1:'Paper',2:'Scissor'}
29+
player_two={0:'Paper',1:'Rock',2:'Scissor'}
30+
while(1):
31+
num1=input("Player one, Enter your choice ")
32+
num2=input("Player two, Enter your choice ")
33+
bit1=int(input("Player one, Enter the secret bit position "))
34+
bit2=int(input("Player two, Enter the secret bit position "))
35+
rock_paper_scissor(num1,num2,bit1,bit2)
36+
ch=input("Do you want to continue? y/n ")
37+
if(ch=='n'):
38+
break
39+
40+
41+
'''
42+
Output Implementation:
43+
44+
Player one, Enter your choice 123
45+
Player two, Enter your choice 567
46+
Player one, Enter the secret bit position 0
47+
Player two, Enter the secret bit position 1
48+
Draw
49+
50+
Do you want to continue? y/n y
51+
Player one, Enter your choice 012
52+
Player two, Enter your choice 234
53+
Player one, Enter the secret bit position 1
54+
Player two, Enter the secret bit position 2
55+
Player one wins!!
56+
57+
Do you want to continue? y/n y
58+
Player one, Enter your choice 345
59+
Player two, Enter your choice 012
60+
Player one, Enter the secret bit position 0
61+
Player two, Enter the secret bit position 2
62+
Player one wins!!
63+
64+
'''

0 commit comments

Comments
 (0)