r/codeHS_Solutions • • Apr 29 '22

10.2.8 Maximum Iterations

1 Upvotes

Does anybody have the code? It's really bothering me. Here is mine. It gives me lossy conversion errors.

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.

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

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

System.out.println("Actual iterations: " + binaryRec(generateArrayOfLength(100), 2, 0, 99));

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

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

System.out.println("Actual iterations: " + binaryRec(generateArrayOfLength(1000), 2, 0, 999));

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

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

System.out.println("Actual iterations: " + binaryRec(generateArrayOfLength(10000), 2, 0, 9999));

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

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

System.out.println("Actual iterations: " + binaryRec(generateArrayOfLength(100000), 2, 0, 99999));

}

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

{

if(begin <= end)

{

int mid = begin + end / 2;

int midVal = array[mid];

count++;

// Base Case

if(target == midVal)

{

return mid;

}

if(target < midVal)

{

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

}

if(target > midVal)

{

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 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

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.2.5: Spell It Out

1 Upvotes

def spell_name(name):

return list(name)


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

8.1.8: Citation

1 Upvotes

def citation(author_name):

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


r/codeHS_Solutions • • Feb 13 '22

8.1.7 Fix This Tuple

1 Upvotes

my_tuple = (0, 1, 2, "hi", 4, 5)

my_tuple = my_tuple[:3] + (3,) + my_tuple[4:]

print(my_tuple)


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 • • Jan 30 '21

Okay so I've tried multiple different things in 8.4.7 double list and nothing is working the instructions are Wright a function that doubles a list "function doubleList(arr)" you should create a new array in the function and return it. I really need help

Post image
1 Upvotes