#Base Code
def binary_search(arr, low, high, x):
if high >= low:
mid = (high + low) // 2
if arr[mid] == x:
return mid
elif arr[mid] > x:
return binary_search(arr, low, mid - 1, x)
else:
return binary_search(arr, mid + 1, high, x)
else:
return -1
arr = [12, 23, 25, 45, 47, 89, 91]
x = 25
result = binary_search(arr, 0, len(arr)-1, x)
if result != -1:
print("The Element is in index", str(result))
else:
print("Element is not present in array")
The Element is in index 2
#Extra Info Version
def binary_search(arr, low, high, x, count=0):
if high >= low:
mid = (high + low) // 2
if arr[mid] == x:
return mid, count
elif arr[mid] > x:
return binary_search(arr, low, mid - 1, x, count + 1)
else:
return binary_search(arr, mid + 1, high, x, count + 1)
else:
return -1, count
arr = [12, 23, 25, 45, 47, 89, 91]
x = 25
result, count = binary_search(arr, 0, len(arr) - 1, x)
if result != -1:
print(f"The element {x} is at index {result} after {count} iterations.")
else:
print("Element is not present in the array.")
The element 25 is at index 2 after 2 iterations.
import random
#List of pokemon 0001-0100 format might be wrong vscode keeps saying there is a bug
pokemon_data = []
pokemon_list = [
{
"name": "Bulbasaur",
"hp": 9,
"attack": 7,
"height": "28",
"weight_in_lbs": "15.2",
"abilities": "Overgrow",
"type": "Grass/Poison",
"weakness": ["Fire", "Psychic", "Ice", "Flying"]
},
{
"name": "Ivysaur",
"hp": 13,
"attack": 8,
"height": "39",
"weight_in_lbs": "28.7",
"abilities": "Overgrow",
"type": "Grass/Poison",
"weakness": ["Fire", "Psychic", "Ice", "Flying"]
},
{
"name": "Venusaur",
"hp": 14,
"attack": 9,
"height": "79",
"weight_in_lbs": "220.5",
"abilities": "Overgrow",
"type": "Grass/Poison",
"weakness": ["Fire", "Psychic", "Ice", "Flying"]
},
{
"name": "Charmander",
"hp": 7,
"attack": 8,
"height": "24",
"weight_in_lbs": "18.7",
"abilities": "Blaze",
"type": "Fire",
"weakness": ["Water", "Rock", "Ground"]
},
{
"name": "Charmeleon",
"hp": 10,
"attack": 9,
"height": "43",
"weight_in_lbs": "41.9",
"abilities": "Blaze",
"type": "Fire",
"weakness": ["Water", "Rock", "Ground"]
}
]
def initPokemons():
#moving pokemon from list to data
item_id=0
for item in pokemon_list:
pokemon_data.append({"id": item_id, "pokemon": item, "upvote": 0, "downvote": 0})
item_id += 1
#adding default upvotes (10)
for i in range(10):
id = getRandomPokemon()['id']
addUpVote(id)
#adding default downvotes (5)
for i in range(5):
id = getRandomPokemon()['id']
addDownVote(id)
#returns all pokemon
def getPokemons():
return(pokemon_data)
#gets a specific pokemon
def getPokemon(id):
return(pokemon_data[id])
#gets a random pokemon
def getRandomPokemon():
return(random.choice(pokemon_data))
#Upvote Pokemon
def bestPokemon():
best = 0
bestID = -1
for pokemon in getPokemons():
if pokemon['upvote'] > best:
best = pokemon['upvote']
bestID = pokemon['id']
return pokemon_data[bestID]
#Downvote Pokemon
def worstPokemon():
worst = 0
worstID = -1
for pokemon in getPokemons():
if pokemon['downvote'] > worst:
worst = pokemon['downvote']
worstID = pokemon['id']
return pokemon_data[worstID]
#Add Upvote
def addUpVote(id):
pokemon_data[id]['upvote'] = pokemon_data[id]['upvote'] +1
return pokemon_data[id]['upvote']
#Add Downvote
def addDownVote(id):
pokemon_data[id]['downvote'] = pokemon_data[id]['downvote'] +1
return pokemon_data[id]['downvote']
def printPokemon(pokemon):
print(pokemon['id'],pokemon['pokemon'],"\n", "upvotes: ", pokemon['upvote'], "\n", "downvote: ", pokemon['downvote'], "\n")
def countPokemons():
return len(pokemon_data)
if __name__ == "__main__":
initPokemons()
best= bestPokemon()
print("Most Upvoted", best['upvote'])
printPokemon(best)
worst= worstPokemon()
print("Most Downvoted", worst['downvote'])
printPokemon(worst)
print("Random Pokemon")
printPokemon(getRandomPokemon())
print("Pokemons Count: " + str(countPokemons()))
print(pokemon_list)
print(pokemon_data)
Most Upvoted 3
3 {'name': 'Charmander', 'hp': 7, 'attack': 8, 'height': '24', 'weight_in_lbs': '18.7', 'abilities': 'Blaze', 'type': 'Fire', 'weakness': ['Water', 'Rock', 'Ground']}
upvotes: 3
downvote: 3
Most Downvoted 3
3 {'name': 'Charmander', 'hp': 7, 'attack': 8, 'height': '24', 'weight_in_lbs': '18.7', 'abilities': 'Blaze', 'type': 'Fire', 'weakness': ['Water', 'Rock', 'Ground']}
upvotes: 3
downvote: 3
Random Pokemon
2 {'name': 'Venusaur', 'hp': 14, 'attack': 9, 'height': '79', 'weight_in_lbs': '220.5', 'abilities': 'Overgrow', 'type': 'Grass/Poison', 'weakness': ['Fire', 'Psychic', 'Ice', 'Flying']}
upvotes: 1
downvote: 0
Pokemons Count: 5
[{'name': 'Bulbasaur', 'hp': 9, 'attack': 7, 'height': '28', 'weight_in_lbs': '15.2', 'abilities': 'Overgrow', 'type': 'Grass/Poison', 'weakness': ['Fire', 'Psychic', 'Ice', 'Flying']}, {'name': 'Ivysaur', 'hp': 13, 'attack': 8, 'height': '39', 'weight_in_lbs': '28.7', 'abilities': 'Overgrow', 'type': 'Grass/Poison', 'weakness': ['Fire', 'Psychic', 'Ice', 'Flying']}, {'name': 'Venusaur', 'hp': 14, 'attack': 9, 'height': '79', 'weight_in_lbs': '220.5', 'abilities': 'Overgrow', 'type': 'Grass/Poison', 'weakness': ['Fire', 'Psychic', 'Ice', 'Flying']}, {'name': 'Charmander', 'hp': 7, 'attack': 8, 'height': '24', 'weight_in_lbs': '18.7', 'abilities': 'Blaze', 'type': 'Fire', 'weakness': ['Water', 'Rock', 'Ground']}, {'name': 'Charmeleon', 'hp': 10, 'attack': 9, 'height': '43', 'weight_in_lbs': '41.9', 'abilities': 'Blaze', 'type': 'Fire', 'weakness': ['Water', 'Rock', 'Ground']}]
[{'id': 0, 'pokemon': {'name': 'Bulbasaur', 'hp': 9, 'attack': 7, 'height': '28', 'weight_in_lbs': '15.2', 'abilities': 'Overgrow', 'type': 'Grass/Poison', 'weakness': ['Fire', 'Psychic', 'Ice', 'Flying']}, 'upvote': 2, 'downvote': 2}, {'id': 1, 'pokemon': {'name': 'Ivysaur', 'hp': 13, 'attack': 8, 'height': '39', 'weight_in_lbs': '28.7', 'abilities': 'Overgrow', 'type': 'Grass/Poison', 'weakness': ['Fire', 'Psychic', 'Ice', 'Flying']}, 'upvote': 2, 'downvote': 0}, {'id': 2, 'pokemon': {'name': 'Venusaur', 'hp': 14, 'attack': 9, 'height': '79', 'weight_in_lbs': '220.5', 'abilities': 'Overgrow', 'type': 'Grass/Poison', 'weakness': ['Fire', 'Psychic', 'Ice', 'Flying']}, 'upvote': 1, 'downvote': 0}, {'id': 3, 'pokemon': {'name': 'Charmander', 'hp': 7, 'attack': 8, 'height': '24', 'weight_in_lbs': '18.7', 'abilities': 'Blaze', 'type': 'Fire', 'weakness': ['Water', 'Rock', 'Ground']}, 'upvote': 3, 'downvote': 3}, {'id': 4, 'pokemon': {'name': 'Charmeleon', 'hp': 10, 'attack': 9, 'height': '43', 'weight_in_lbs': '41.9', 'abilities': 'Blaze', 'type': 'Fire', 'weakness': ['Water', 'Rock', 'Ground']}, 'upvote': 2, 'downvote': 0}]
import random
joke_data = []
joke_list = [
"If you give someone a program... you will frustrate them for a day; if you teach them how to program... you will "
"frustrate them for a lifetime.",
{"name": "Pikachu", "hp": 35, "level": 15, "type": "Electric"},
"UNIX is basically a simple operating system... but you have to be a genius to understand the simplicity.",
"Enter any 11-digit prime number to continue.",
"If at first you don't succeed; call it version 1.0.",
"Java programmers are some of the most materialistic people I know, very object-oriented",
"The oldest computer can be traced back to Adam and Eve. It was an apple but with extremely limited memory. Just "
"1 byte. And then everything crashed.",
"Q: Why did Wi-Fi and the computer get married? A: Because they had a connection",
"Bill Gates teaches a kindergarten class to count to ten. 1, 2, 3, 3.1, 95, 98, ME, 2000, XP, Vista, 7, 8, 10.",
"Q: What’s a aliens favorite computer key? A: the space bar!",
"There are 10 types of people in the world: those who understand binary, and those who don’t.",
"If it wasn't for C, we’d all be programming in BASI and OBOL.",
"Computers make very fast, very accurate mistakes.",
"Q: Why is it that programmers always confuse Halloween with Christmas? A: Because 31 OCT = 25 DEC.",
"Q: How many programmers does it take to change a light bulb? A: None. It’s a hardware problem.",
"The programmer got stuck in the shower because the instructions on the shampoo bottle said: Lather, Rinse, Repeat.",
"Q: What is the biggest lie in the entire universe? A: I have read and agree to the Terms and Conditions.",
'An SQL statement walks into a bar and sees two tables. It approaches, and asks may I join you?'
]
# Initialize jokes
def initJokes():
# setup jokes into a dictionary with id, joke, haha, boohoo
item_id = 0
for item in joke_list:
joke_data.append({"id": item_id, "joke": item, "haha": 0, "boohoo": 0})
item_id += 1
# prime some haha responses
for i in range(10):
id = getRandomJoke()['id']
addJokehaha(id)
# prime some haha responses
for i in range(5):
id = getRandomJoke()['id']
addJokeboohoo(id)
# Return all jokes from joke_data
def getJokes():
return(joke_data)
# Joke getter
def getJoke(id):
return(joke_data[id])
# Return random joke from joke_data
def getRandomJoke():
return(random.choice(joke_data))
# Liked joke
def favoriteJoke():
best = 0
bestID = -1
for joke in getJokes():
if joke['haha'] > best:
best = joke['haha']
bestID = joke['id']
return joke_data[bestID]
# Jeered joke
def jeeredJoke():
worst = 0
worstID = -1
for joke in getJokes():
if joke['boohoo'] > worst:
worst = joke['boohoo']
worstID = joke['id']
return joke_data[worstID]
# Add to haha for requested id
def addJokehaha(id):
joke_data[id]['haha'] = joke_data[id]['haha'] + 1
return joke_data[id]['haha']
# Add to boohoo for requested id
def addJokeboohoo(id):
joke_data[id]['boohoo'] = joke_data[id]['boohoo'] + 1
return joke_data[id]['boohoo']
# Pretty Print joke
def printJoke(joke):
print(joke['id'], joke['joke'], "\n", "haha:", joke['haha'], "\n", "boohoo:", joke['boohoo'], "\n")
# Number of jokes
def countJokes():
return len(joke_data)
# Test Joke Model
if __name__ == "__main__":
initJokes() # initialize jokes
# Most likes and most jeered
best = favoriteJoke()
print("Most liked", best['haha'])
printJoke(best)
worst = jeeredJoke()
print("Most jeered", worst['boohoo'])
printJoke(worst)
# Random joke
print("Random joke")
printJoke(getRandomJoke())
# Count of Jokes
print("Jokes Count: " + str(countJokes()))
Most liked 2
11 If it wasn't for C, we’d all be programming in BASI and OBOL.
haha: 2
boohoo: 0
Most jeered 1
0 If you give someone a program... you will frustrate them for a day; if you teach them how to program... you will frustrate them for a lifetime.
haha: 0
boohoo: 1
Random joke
15 The programmer got stuck in the shower because the instructions on the shampoo bottle said: Lather, Rinse, Repeat.
haha: 0
boohoo: 0
Jokes Count: 18
import random
#List of pokemon 0001-0100 format might be wrong vscode keeps saying there is a bug
pokemon_data = []
pokemon_list = [
{
"name": "Bulbasaur",
"hp": 9,
"attack": 7,
"height": "28",
"weight_in_lbs": "15.2",
"abilities": "Overgrow",
"type": "Grass/Poison",
"weakness": ["Fire", "Psychic", "Ice", "Flying"]
},
{
"name": "Ivysaur",
"hp": 13,
"attack": 8,
"height": "39",
"weight_in_lbs": "28.7",
"abilities": "Overgrow",
"type": "Grass/Poison",
"weakness": ["Fire", "Psychic", "Ice", "Flying"]
},
{
"name": "Venusaur",
"hp": 14,
"attack": 9,
"height": "79",
"weight_in_lbs": "220.5",
"abilities": "Overgrow",
"type": "Grass/Poison",
"weakness": ["Fire", "Psychic", "Ice", "Flying"]
},
{
"name": "Charmander",
"hp": 7,
"attack": 8,
"height": "24",
"weight_in_lbs": "18.7",
"abilities": "Blaze",
"type": "Fire",
"weakness": ["Water", "Rock", "Ground"]
},
{
"name": "Charmeleon",
"hp": 10,
"attack": 9,
"height": "43",
"weight_in_lbs": "41.9",
"abilities": "Blaze",
"type": "Fire",
"weakness": ["Water", "Rock", "Ground"]
}
]
def initPokemons():
#moving pokemon from list to data
item_id=0
for item in pokemon_list:
pokemon_data.append({"id": item_id, "pokemon": item, "upvote": 0, "downvote": 0})
item_id += 1
#adding default upvotes (10)
for i in range(10):
id = getRandomPokemon()['id']
addUpVote(id)
#adding default downvotes (5)
for i in range(5):
id = getRandomPokemon()['id']
addDownVote(id)
#returns all pokemon
def getPokemons():
return(pokemon_data)
#gets a specific pokemon
def getPokemon(id):
return(pokemon_data[id])
#gets a random pokemon
def getRandomPokemon():
return(random.choice(pokemon_data))
#Upvote Pokemon
def bestPokemon():
best = 0
bestID = -1
for pokemon in getPokemons():
if pokemon['upvote'] > best:
best = pokemon['upvote']
bestID = pokemon['id']
return pokemon_data[bestID]
#Downvote Pokemon
def worstPokemon():
worst = 0
worstID = -1
for pokemon in getPokemons():
if pokemon['downvote'] > worst:
worst = pokemon['downvote']
worstID = pokemon['id']
return pokemon_data[worstID]
#Add Upvote
def addUpVote(id):
pokemon_data[id]['upvote'] = pokemon_data[id]['upvote'] +1
return pokemon_data[id]['upvote']
#Add Downvote
def addDownVote(id):
pokemon_data[id]['downvote'] = pokemon_data[id]['downvote'] +1
return pokemon_data[id]['downvote']
def printPokemon(pokemon):
print(pokemon['id'],pokemon['pokemon'],"\n", "upvotes: ", pokemon['upvote'], "\n", "downvote: ", pokemon['downvote'], "\n")
def countPokemons():
return len(pokemon_data)
if __name__ == "__main__":
initPokemons()
best= bestPokemon()
print("Most Upvoted", best['upvote'])
printPokemon(best)
worst= worstPokemon()
print("Most Downvoted", worst['downvote'])
printPokemon(worst)
print("Random Pokemon")
printPokemon(getRandomPokemon())
print("Pokemons Count: " + str(countPokemons()))
print(pokemon_data)
Most Upvoted 3
3 {'name': 'Charmander', 'hp': 7, 'attack': 8, 'height': '24', 'weight_in_lbs': '18.7', 'abilities': 'Blaze', 'type': 'Fire', 'weakness': ['Water', 'Rock', 'Ground']}
upvotes: 3
downvote: 0
Most Downvoted 2
1 {'name': 'Ivysaur', 'hp': 13, 'attack': 8, 'height': '39', 'weight_in_lbs': '28.7', 'abilities': 'Overgrow', 'type': 'Grass/Poison', 'weakness': ['Fire', 'Psychic', 'Ice', 'Flying']}
upvotes: 2
downvote: 2
Random Pokemon
0 {'name': 'Bulbasaur', 'hp': 9, 'attack': 7, 'height': '28', 'weight_in_lbs': '15.2', 'abilities': 'Overgrow', 'type': 'Grass/Poison', 'weakness': ['Fire', 'Psychic', 'Ice', 'Flying']}
upvotes: 0
downvote: 1
Pokemons Count: 5
[{'id': 0, 'pokemon': {'name': 'Bulbasaur', 'hp': 9, 'attack': 7, 'height': '28', 'weight_in_lbs': '15.2', 'abilities': 'Overgrow', 'type': 'Grass/Poison', 'weakness': ['Fire', 'Psychic', 'Ice', 'Flying']}, 'upvote': 0, 'downvote': 1}, {'id': 1, 'pokemon': {'name': 'Ivysaur', 'hp': 13, 'attack': 8, 'height': '39', 'weight_in_lbs': '28.7', 'abilities': 'Overgrow', 'type': 'Grass/Poison', 'weakness': ['Fire', 'Psychic', 'Ice', 'Flying']}, 'upvote': 2, 'downvote': 2}, {'id': 2, 'pokemon': {'name': 'Venusaur', 'hp': 14, 'attack': 9, 'height': '79', 'weight_in_lbs': '220.5', 'abilities': 'Overgrow', 'type': 'Grass/Poison', 'weakness': ['Fire', 'Psychic', 'Ice', 'Flying']}, 'upvote': 2, 'downvote': 0}, {'id': 3, 'pokemon': {'name': 'Charmander', 'hp': 7, 'attack': 8, 'height': '24', 'weight_in_lbs': '18.7', 'abilities': 'Blaze', 'type': 'Fire', 'weakness': ['Water', 'Rock', 'Ground']}, 'upvote': 3, 'downvote': 0}, {'id': 4, 'pokemon': {'name': 'Charmeleon', 'hp': 10, 'attack': 9, 'height': '43', 'weight_in_lbs': '41.9', 'abilities': 'Blaze', 'type': 'Fire', 'weakness': ['Water', 'Rock', 'Ground']}, 'upvote': 3, 'downvote': 2}]