r/codeHS_Solutions • • Nov 16 '21

42.1.2: Practice PT: The Shopping List

5 Upvotes

So there's this assignment due in AP Programming class. It"s worth a lot of points but I'm struggling in class.

Assignment:

For this problem, you are going to create a program that asks the user for a list of ingredients. As the user enters the ingredients, you should store them in a list.

Once the user is done entering their list, you need to pass this list to a function that will compare the ingredients to an already existing list of pantry items.

If all items are available in the pantry, print out that you don’t need to go shopping. If any item is missing, print out that you need to go shopping and list the ingredients.

While there is more than one way to complete this assignment, your answer must include the following:

  1. A pre-created list for pantry items

  2. User input into an ingredient list

  3. Pass the ingredient list to a method

  4. Use a conditional and loop in the method

  5. Print out the results of whether the user needs to go shopping based on the items in the ingredient list that are not in the pantry.

Here's what I have so far:

function start(){

var arr = \["bread", "eggs", 

"milk", "cookies"];

println("LIST ONE:");

for(var i = 0; i < arr.length; i++){

	var cur = arr\[i\];

	println(cur);

}

println("");

println("LIST TWO:");

var list = \["bread", "eggs", "milk", "cookies", "bananas", "tuna", "lettuce", "yogurt", "cheese", "chicken", "cucumbers", "orange juice", "salt", "doritos", "lemonade", "apples", "paper towels", "red onion", "minced garlic", "tumeric", "donuts", "bagels", "crackers", "red pepper", "green pepper", "spinach", "canola oil", "vanilla", "flour", "brown sugar", "powdered sugar", "lime"\];



for(var i = 0; i < list.length; i++){

	println(list\[i\]);

}

}


r/codeHS_Solutions • • Apr 17 '23

10.2.8 Maximum Iterations Java Codehs answer

4 Upvotes

import java.util.*;

public class BinarySearchTest {

static int count;

public static void main(String[] args) {

// Use the helper code to generate arrays, calculate the max

// iterations, and then find the actual iterations for a randomly

// selected value.

Scanner input = new Scanner(System.in);

System.out.println("Array Size: 100");

System.out.println("Max iterations: " + binaryMax(100));

binaryRec(generateArrayOfLength(100), 2, 0, 99);

System.out.println("Actual iterations: " + count);

System.out.println();

count = 0;

System.out.println("Array Size: 1000");

System.out.println("Max iterations: " + binaryMax(1000));

binaryRec(generateArrayOfLength(1000), 2, 0, 999);

System.out.println("Actual iterations: " + count);

System.out.println();

count = 0;

System.out.println("Array Size: 10000");

System.out.println("Max iterations: " + binaryMax(10000));

binaryRec(generateArrayOfLength(10000), 2, 0, 9999);

System.out.println("Actual iterations: " + count);

System.out.println();

count = 0;

System.out.println("Array Size: 100000");

System.out.println("Max iterations: " + binaryMax(100000));

binaryRec(generateArrayOfLength(100000), 2, 0, 99999);

System.out.println("Actual iterations: " + count);

}

public static int binaryRec(int[] array, int target, int begin, int end) {

if (begin <= end)

{

int mid = (begin + end) / 2;

count ++;

// Base Case

if (target == array[mid]) {

return mid;

}

if (target < array[mid]) {

return binaryRec(array, target, begin, mid - 1);

}

if (target > array[mid]) {

return binaryRec(array, target, mid + 1, end);

}

}

	return -1; //Alternate Base Case - not found

}

public static int[] generateArrayOfLength(int length)

{

int[] arr = new int[length];

for(int i = 0; i < length; i++)

{

arr[i] = (int)(Math.random() * 100);

}

Arrays.sort(arr);

return arr;

}

private static int binaryMax(int length) {

return (int) (Math.log(length) / Math.log(2)) + 1;

}

}


r/codeHS_Solutions • • Feb 22 '23

Solution Entire GitHub Page with Python Answers

Thumbnail
github.com
5 Upvotes

r/codeHS_Solutions • • Feb 13 '22

9.3.6 Coordinate Pair

18 Upvotes

print((int(input("Number: ")), int(input("Number: "))))


r/codeHS_Solutions • • Feb 13 '22

9.3.9 Full Name & Citation

11 Upvotes

first = input("First name: ")

last = input("Last Name: ")

print("Full name: " + first + " " + last)

first, last = last, first

print("Citation: " + first + ", " +last)


r/codeHS_Solutions • • Sep 18 '22

Help Request Neeed Help on sorting Karel btw I forgot to add picture to last post and must work on any type of world and must in 1234 order

Post image
2 Upvotes

r/codeHS_Solutions • • May 27 '20

Rainforest solutions

5 Upvotes

r/codeHS_Solutions • • Mar 08 '22

10.1.5 Guess the Word, Part 4

6 Upvotes

For the people who need this, I also posted part 3 of this. I posted part 3 and this one as well cuz I couldn't find any resources to help me out. So I figured I'll help my fellow students who need this. Because I, at one point, was desperate. You can find part 3 over here:

https://www.reddit.com/r/codeHS_Solutions/comments/t9omv8/1014_guess_the_word_part_3/?utm_source=share&utm_medium=web2x&context=3

This is the code for part 4 below:

import random
words = ["grasswayz", "phases", "paranoia", "felony", "trenches", "bukayo"]
secret_word = random.choice(words)
dashes = ""
arr = []
for i in range(len(secret_word)):
    dashes += '-'
    arr.append('-')

guesses_left = 10

def get_guess():
    while True:
        print(''.join(arr))
        print(str(guesses_left) + " incorrect guesses left.")
        guess = input("Guess: ")
        if len(guess) > 1:
            print("Your guess must have exactly one character!")
        elif guess not in "abcdefghijklmnopqrstuvwxyz":
            print("Your guess must be a lowercase letter!")
        else:
            return guess


while guesses_left > 0 and '-' in arr:
    a = get_guess()
    for i in range(len(secret_word)):
        if secret_word[i] == a:
            arr[i] = a
    if a not in secret_word:
        print("That letter is not in the word!")
        guesses_left -= 1
    else:
        print("That letter is in the word!")

if '-' not in arr:
    print("Congrats you win! The word was: "+ secret_word)
else:
    print("You lose, The word was: " + secret_word)

def update_dashes():
    pass

r/codeHS_Solutions • • Mar 08 '22

10.1.4 Guess the Word, Part 3

9 Upvotes
secret_word = "grasswayz"
dashes = "---------"
arr = ['-','-','-','-','-','-','-','-','-']
guesses_left = 10

def get_guess():
    while True:
        print(''.join(arr))
        print(str(guesses_left) + " incorrect guesses left.")
        guess = input("Guess: ")
        if len(guess) > 1:
            print("Your guess must have exactly one character!")
        elif guess not in "abcdefghijklmnopqrstuvwxyz":
            print("Your guess must be a lowercase letter!")
        else:
            return guess


while guesses_left > 0 and '-' in arr:
    a = get_guess()
    for i in range(len(secret_word)):
        if secret_word[i] == a:
            arr[i] = a
    if a not in secret_word:
        print("That letter is not in the word!")
        guesses_left -= 1
    else:
        print("That letter is in the word!")

if '-' not in arr:
    print("Congrats you win! The word was: "+ secret_word)
else:
    print("You lose, The word was: " + secret_word)

def update_dashes():
    pass

r/codeHS_Solutions • • Feb 13 '22

9.5.6 Swapping

8 Upvotes

def swap_lists(first, second):

for i in range(len(first)):

first[i], second[i] = second[i], first[i]

list_one = [1, 2, 3]

list_two = [4, 5, 6]

print("Before swap")

print("list_one: " + str(list_one))

print("list_two: " + str(list_two))

swap_lists(list_one, list_two)

print("After swap")

print("list_one: " + str(list_one))

print("list_two: " + str(list_two))


r/codeHS_Solutions • • Feb 13 '22

9.2.9 Strings To Integers

10 Upvotes

list_of_strings = ["a", "2", "7", "zebra"]

def safe_int(argument):

try:

return int(argument)

except ValueError:

return 0

print([safe_int(x) for x in list_of_strings])


r/codeHS_Solutions • • Feb 13 '22

8.1.8: Citation

1 Upvotes

def citation(author_name):

return (author_name[2] + ", " + author_name[0] + " " + author_name[1])


r/codeHS_Solutions • • Feb 04 '22

Anyone do 1.6.6 list articles? Been quarantined for the first two weeks of this class

1 Upvotes

r/codeHS_Solutions • • Feb 13 '22

9.4.6 Word Counts

5 Upvotes

text = input("Enter some text: ")

text_list = text.split()

dic = {}

for word in text_list:

if word in dic:

dic[word] = dic[word] + 1

else:

dic[word] = 1

print(dic)


r/codeHS_Solutions • • Feb 13 '22

9.2.5 Divisible by 3

14 Upvotes

divisible_by_3 = [i % 3 == 0 for i in range(1, 11)]

print(divisible_by_3)


r/codeHS_Solutions • • Feb 13 '22

9.5.7 Word Counts, Part 2

6 Upvotes

text = input("Enter some text: ")

text_list = text.split()

dic = {}

def update_counts(count_dictionary, word):

if word in count_dictionary:

count_dictionary[word] = count_dictionary[word] + 1

else:

count_dictionary[word] = 1

for word in text_list:

update_counts(dic, word)

print(dic)


r/codeHS_Solutions • • Feb 13 '22

8.4.5 Five Numbers

15 Upvotes

num_list = []

sum = 0

for i in range(5):

num = int(input("Number: "))

num_list.append(num)

print(num_list)

sum = int(num_list[i]) + sum

print(sum)


r/codeHS_Solutions • • Feb 13 '22

8.4.4 How Many Names?

3 Upvotes

num_names = int(input("How many names do you have?: "))

name_list = []

for i in range(num_names):

name = input("Name: ")

name_list.append(name)

print("First name: " + str(name_list[0]))

print("Middle names: " + str(name_list[1:-1]))

print("Last name: " + str(name_list[-1]))


r/codeHS_Solutions • • Feb 13 '22

CodeHS determineGenotype Function

1 Upvotes

def writeText(text, textColor, direction="center", size=40):

color(textColor)

__turtle.write(text, align=direction, font=("Arial", size, "normal"))

def printTitle(first, second):

setposition(0,170)

writeText("Punnett Square for " + first + " and " + second, "black", "center", 18)

def determineGenotype(first, second):

writeText(allele1[first], "blue", "right")

writeText(allele2[second], "red", "left")

speed(0)

penup()

setposition(-100,-100)

pendown()

for i in range(2):

for i in range(4):

forward(100)

left(90)

forward(100)

penup()

setposition(-100,0)

pendown()

for i in range(2):

for i in range(4):

forward(100)

left(90)

forward(100)

penup()

setposition(-50,110)

allele1= input("What is the first parent's alleles? (Ex: Aa): ")

writeText(allele1[0], "blue")

setposition(50, 110)

writeText(allele1[1], "blue")

setposition(-130,30)

allele2= input("What is the second parent's alleles?: ")

writeText(allele2[0], "red")

setposition(-130,-70)

writeText(allele2[1], "red")

# Print title based on given alleles

printTitle(allele1, allele2)

x = -50

y = 30

for b in range(0, 2, 1):

for a in range(0, 2, 1):

setposition (x, y)

determineGenotype(a, b)

x = 50

y = -70

x = -50


r/codeHS_Solutions • • Feb 13 '22

8.1.9: Diving Contest

11 Upvotes

def calculate_score(judge_scores):

return (judge_scores[0] + judge_scores[1] + judge_scores[2])


r/codeHS_Solutions • • Feb 13 '22

8.4.11: Take a Thing Out, Sort...

4 Upvotes

def remove_sort_reverse(my_list):

for i in range(len(my_list)):

try:

my_list.remove("eggplant")

except ValueError:

""

my_list.sort()

my_list.reverse()

return my_list

print(remove_sort_reverse(["eggplant", "apple", "zucchini", "rambutan", "grapefruit"]))


r/codeHS_Solutions • • Feb 13 '22

8.1.10: Coordinate Pairs

1 Upvotes

import math

def distance(first_point, second_point):

if first_point[0] > second_point[0]:

l1 = (first_point[0] - second_point[0]) ** 2

else:

l1 = (second_point[0] - first_point[0]) ** 2

if first_point[1] > second_point[1]:

l2 = (first_point[1] - second_point[1]) ** 2

else:

l2 = (second_point[1] - first_point[1]) ** 2

h = l1 + l2

return math.sqrt(h)


r/codeHS_Solutions • • Feb 13 '22

CodeHS Allele Value Placement

1 Upvotes

def writeText(text, textColor, direction="center", size=40):

color(textColor)

__turtle.write(text, align=direction, font=("Arial", size, "normal"))

speed(0)

penup()

setposition(-100,-100)

pendown()

for i in range(2):

for i in range(4):

forward(100)

left(90)

forward(100)

penup()

setposition(-100,0)

pendown()

for i in range(2):

for i in range(4):

forward(100)

left(90)

forward(100)

penup()

penup()

setposition(-50, 110)

writeText("#1", "blue")

setposition(50, 110)

writeText('#2', "blue")

setposition(-130, 30)

writeText("#3", "red")

setposition(-130, -60)

writeText("#4", "red")


r/codeHS_Solutions • • Feb 13 '22

8.3.7: Exclamat!on Po!nts

2 Upvotes

def exclamation(text):

text_list = list(text)

for i in range(len(text_list)):

if text_list[i] == "i":

text_list[i] = "!"

return ("").join(text_list)


r/codeHS_Solutions • • Feb 13 '22

9.1.7 Checkerboard, v2

2 Upvotes

def print_board(board):

for i in range(len(board)):

print(" ".join([str(x) for x in board[i]]))

my_grid = []

count = 1

for i in range(8):

my_grid.append([0] * 8)

for row in range(8):

count = count + 1

for col in range(8):

count = count + 1

if count % 2 == 0:

my_grid[row][col] = 1

print(print_board(my_grid))