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

9.3.7 Slopes

6 Upvotes

coordinate_pairs = []

for i in range(5):

x = int(input("Input x coordinate: "))

y = int(input("Input y coordinate:"))

coordinate_pairs.append((x,y))

def slope(x1, x2, y1, y2):

return((y2 - y1) / (x2 - x1))

for i in range(4):

x1, y1 = coordinate_pairs[i]

x2, y2 = coordinate_pairs[i+1]

print("Slope between " + str(coordinate_pairs[i]) + " and " + str(coordinate_pairs[i + 1]) + ": " + str(slope(x1, x2, y1, y2)))


r/codeHS_Solutions • • Feb 13 '22

9.1.8 Checkerboard, v3

6 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 row < 3 or row > 4:

if count % 2 == 0:

my_grid[row][col] = 1

print(print_board(my_grid))


r/codeHS_Solutions • • Jun 01 '21

1.9.7: Ball in Each Corner

6 Upvotes

/*This function allows Karel to put a ball in each corner and

*end up on one, one facing east.

*Precondition: Karel is on one, one facing east.

*Postcondition: There will be a ball in each corner of the map and Karel

*be on one, one facing east.

*/

function start () {

for(var i = 0; i < 5; i++) {

move();

}

putBallTurnLeft();

for(var i = 0; i < 5; i++) {

move();

}

putBallTurnLeft();

for(var i = 0; i < 5; i++) {

move();

}

putBallTurnLeft();

for(var i = 0; i < 5; i++) {

move();

}

putBallTurnLeft();

}

/*This function allows Karel to put one ball and turn left at each corner.

*Precondition: Karel is at a corner where he needs to put a ball and

*turn left.

*Postcondition: Karel will place a ball and turn left.

*/

function putBallTurnLeft () {

putBall();

turnLeft();

}


r/codeHS_Solutions • • Jun 01 '21

1.7.4: The Two Towers + Comments

6 Upvotes

/*This program lets Karel build two towers and end up on top of the second

*tower.

*Precondition: Karel will start on one, one.

*Postcondition: Karel will be on top of the second tower and facing east.

*/

function start() {

move();

buildThree();

turnRight();

move();

turnRight();

move();

move();

turnLeft();

move();

buildThree();

move();

turnRight();

}

/*This function lets Karel build the tower.

*Precondition: Karel is facing east on the first level of the tower.

*Postcondition: Karel is facing north on the third level of the tower.

*/

function buildThree() {

putBall();

turnLeft();

move();

putBall();

move();

putBall();

}

// This function helps Karel turn right

/*This function helps Karel turn right

*Precondition: Karel needs to turn right

*Postcondition: Karel ends up turning right by turning left 3 times

*/

function turnRight() {

turnLeft();

turnLeft();

turnLeft();

}


r/codeHS_Solutions • • Jun 01 '21

1.3.5: Fireman Karel

6 Upvotes

turnRight();

move();

move();

move();

turnLeft();

function turnRight() {

turnLeft();

turnLeft();

turnLeft();

}


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.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 • • Dec 13 '21

REQUEST MEGATHREAD

5 Upvotes

A megathread for answer requests. If you want unposted answers, this is the place to ask for them.

If you have answers to anything here be sure to also make a post for it too.


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 • • Jun 01 '21

1.3.4: Slide Karel

5 Upvotes

putBall();

move();

turnRight();

move();

putBall();

move();

turnLeft();

move();

putBall();

function turnRight(){

turnLeft();

turnLeft();

turnLeft();

}


r/codeHS_Solutions • • Mar 09 '21

CodeHS 17.1.4: Grades

5 Upvotes

function start(){

letterGrade(100);

letterGrade(83);

letterGrade(68);

letterGrade(91);

letterGrade(47);

letterGrade(79);

//Add some more! (THESE ARE OPTIONAL)

letterGrade(44);

letterGrade(30);

letterGrade(68);

letterGrade(73);

letterGrade(97);

letterGrade(88);

}

function letterGrade(score){

if(score == 100){

println("A+");

}else

if(score < 60){

println("F");

}else{

var letter = checkLetter(score / 10);

var sign = checkSign(score % 10);

println(letter + sign);

}

}

function checkLetter(tens){

if(tens >= 9){

return "A";

}else

if(tens >= 8){

return "B";

}else

if(tens >= 7){

return "C";

}else{

return "D";

}

}

function checkSign(ones){

if(ones <= 2){

return "-";

}else

if(ones >= 7){

return "+";

}else{

return " ";

}

}


r/codeHS_Solutions • • May 27 '20

Rainforest solutions

5 Upvotes

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 • • May 22 '22

12.1.4 The Rectangle Class, Part 1 answer

Post image
4 Upvotes

r/codeHS_Solutions • • Feb 13 '22

9.4.5 Phone Book

4 Upvotes

my_dictionary = {}

while True:

user = input("Enter a name: ")

if user == "":

break

elif user in my_dictionary:

print("Phone number: " + my_dictionary[user])

else:

phone = input("Enter " + user + "'s phone number: ")

my_dictionary[user] = phone

print(my_dictionary)


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 • • Oct 21 '21

Does anyone has 5.8.6 Special Vendors HTML

4 Upvotes

Introduction

You’ve seen several examples of special selectors. You can find a complete list here: https://www.w3schools.com/CSSref/css_selectors.asp

For this exercise, you will use the :first-child
selector.

Your Task

Add a CSS rule that selects the first li
element that is the first-child
. This will select the first item in each of the ul
lists on the page.
Make the first item in each list have the font color #32ed96
and the font size 24px.

Use the :first-child
selector. This selector picks the element tag that is the first child of another element. For instance, p:first-child
selects all of the <p>
tags that come first inside of another tag.


r/codeHS_Solutions • • Jun 01 '21

1.9.6: Dizzy Karel

4 Upvotes

//This program allows Karel to spin eight times.

function start(){

/*This loop makes Karel spin eight times.

*Precondition: Karel is on one, one facing east.

*Postcondition: Karel will be in the same position.

*/

for (var i = 0; i< 32; i++) {

turnLeft();

}

}


r/codeHS_Solutions • • Jun 01 '21

1.8.4: The Two Towers + SuperKarel

4 Upvotes

/*This program will help Karel build two towers and end up on top

*of the seond tower facing east.

*Precondition: Karel will start on one, one.

*Postcondition: Karel will be on top of the second tower and facing east.

*/

*/

function start(){

move();

buildThree();

turnRight();

move();

turnRight();

move();

move();

turnLeft();

move();

buildThree();

move();

turnRight();

}

/*This function will help Karel build one tower

*Precondition: Karel is on the first level of his tower facing east

*Postcondition: Karel will be on the third level of his tower facing

*north

*/

function buildThree() {

putBall();

turnLeft();

move();

putBall();

move();

putBall();

}


r/codeHS_Solutions • • Jun 01 '21

1.2.5: Pyramid of Karel

4 Upvotes

putBall();

move();

putBall();

move();

putBall();

turnLeft();

move();

putBall();

turnLeft();

move();

putBall();

turnLeft();

turnLeft();

turnLeft();

move();

turnLeft();

turnLeft();

turnLeft();

move();

putBall();


r/codeHS_Solutions • • Apr 23 '21

4.5.5 Teleporting Ball

4 Upvotes

I put this code in and its telling me line 7 "ball.setPosition(100, 100); has an error and that it can't read it what do i do to fix it.

var dx = 4;

var dy = 4;

/* This program has a ball bounce around the screen. */

function start(){

***ball.setPosition(100, 100);***

add(ball);

setTimer(draw, 20);

mouseClickMethod(tp);

}

function tp(e){

ball.setPosition(e.getX(), e.getY());

ball.setColor(Randomizer.nextColor());

}

function draw(){

checkWalls();

ball.move(dx, dy);

}

function checkWalls(){

// Bounce off right wall

if(ball.getX() + ball.getRadius() > getWidth()){

	dx = -dx;

}



// Bounce off left wall

if(ball.getX() - ball.getRadius() < 0){

	dx = -dx;

}



// Bounce off bottom wall

if(ball.getY() + ball.getRadius() > getHeight()){

	dy = -dy;

}



// Bounce off top wall

if(ball.getY() - ball.getRadius() < 0){

	dy = -dy;

}

}


r/codeHS_Solutions • • Feb 04 '21

If anyone wants solutions (Any codehs course)..

4 Upvotes

If anyone wants solutions or answers to CodeHS, message me privately or join the discord

https://discord.gg/gNTmBrdyAv

If you join the discord, I can help you with quizzes too


r/codeHS_Solutions • • Sep 30 '17

[Python - Rainforest] Intro to Programming with Turtle Graphics

4 Upvotes

Introduction

Well I guess if nobody else is going to get this ball rolling, I'd be happy to be the first.

I'm going to post what I have of the Python course because that's what's most relevant to me at the moment. Then if I feel up to it I'll post everything I have for Java(which is most of it).

Before I start I should say, you're using this repository at your own risk. If you don't know/aren't comfortable with Python yet then you should do the work yourself so that when finals come around, you won't be left staring at your monitor not knowing how the fuck to write an if statement. I also have to say that I'm in no way responsible for any trouble you get in if you are caught copy/pasting my code. Now, with that out of the way, here it is...

Lesson 1

2.1.7: Square

forward(50)

left(90)

forward(50)

left(90)

forward(50)

left(90)

forward(50)

left(90)

2.1.8: Diamond

left(45)

forward(50)

left(90)

forward(50)

left(90)

forward(50)

left(90)

forward(50)

left(90)

2.1.9: Staircase

left(90)

forward(50)

right(90)

forward(50)

left(90)

forward(50)

right(90)

forward(50)

left(90)

Lesson 2

2.2.5: Alternating Colors

forward(50)

left(90)

color("red")

forward(50)

left(90)

color("black")

forward(50)

left(90)

color("red")

forward(50)

left(90)

color("black")

2.2.6: Rainbow

color("red")

forward(20)

color("orange")

forward(20)

color("yellow")

forward(20)

color("green")

forward(20)

color("blue")

forward(20)

color("indigo")

forward(20)

color("violet")

forward(20)

2.2.7: Rainbow, Part 2

color("red")

forward(40)

left(60)

color("orange")

forward(40)

left(60)

color("yellow")

forward(40)

left(60)

color("green")

forward(40)

left(60)

color("blue")

forward(40)

left(60)

color("violet")

forward(40)

left(60)

Lesson 3

2.3.5: Triangle

for i in range(3):

    left(120)

    forward(50)

2.3.6: Hexagon

for i in range(6):

    forward(50)

    left(60)

2.3.7: Staircase, Part 2

left(90)

for i in range(4):
    forward(30)
    right(90)
    forward(30)
    left(90)

2.3.8: Circle

for i in range(360):
    forward(1)
    left(1)

Lesson 4

2.4.4: Staircase, Part 3

left(90)

#This function would work if the code check for this assignment wasn't so autistic. It's not that important but it's worth noting...
#def draw_step(x):
#    for i in range(x):
#        forward(30)
#        right(90)
#        forward(30)
#        left(90)

def draw_step():
    forward(30)
    right(90)
    forward(30)
    left(90)

draw_step()
draw_step()
draw_step()
draw_step()

2.4.5: Hexagon, Part 2

def draw_edge():
    for i in range(6):
        forward(50)
        left(60)
    
draw_edge()

2.4.6: Alternating Colors, Part 2

def draw_red_edge():
    color("red")
    forward(50)

def draw_black_edge():
    color("black")
    forward(50)


draw_black_edge()
left(90)
draw_red_edge()
left(90)
draw_black_edge()
left(90)
draw_red_edge()
left(90)

2.4.7: Fenceposts

def make_fencepost():
    forward(10)
    left(90)
    forward(30)
    right(180)
    forward(30)
    left(90)
    
for i in range(6):
    make_fencepost()

2.4.8: Diamonds

def draw_diamond():
    left(45)
    for i in range(4):
        forward(20)
        left(90)
    right(45)

for i in range(4):
    draw_diamond()
    forward(50)

2.5.1: Get Creative

Just press continue


r/codeHS_Solutions • • May 04 '23

Help Request Help on 2.15.4 Three Strings

Post image
3 Upvotes

I need help on this problem I was able to find a solution for the gofishturtle but it’s calling out the pepper mint is equal to instead of the is not equal to on turtlegofish