Last active
April 1, 2020 03:57
-
-
Save harrisonmalone/5807dad75f7a53c6f92294b0230605d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Q13 | |
# The code snippet below looks for the first two elements that are out of order and swaps them; however, it is not producing the correct results. Rewrite the code so that it works correctly. | |
arr = [5, 22, 29, 39, 19, 51, 78, 96, 84] | |
index = 0 | |
copy = arr[0..arr.length - 1] | |
while index < arr.length - 1 | |
# make a copy of the arr | |
if arr[index] > arr[index + 1] | |
# instead of using the arr values in the assignment use the copy values | |
arr[index] = arr[index + 1] | |
arr[index + 1] = arr[index] | |
end | |
index += 1 | |
end | |
# Q14 | |
# Demonstrate your algorithmic thinking through completing the following two tasks, in order: | |
# 1. Create a flowchart to outline the steps for listing all prime numbers between 1 and 100 (inclusive). Your flowchart should make use of standard conventions for flowcharts to indicate processes, tasks, actions, or operations | |
# 2. Write pseudocode for the process outlined in your flowchart | |
# starting at number 2 for the first number to test | |
# make a loop that will run all the way until 100 | |
# increment the number by 1 every time the loop runs | |
# initialize a new divider and set it at 2 | |
# inside of the outer loop have an inner loop that will run the number of times for the number that we're currently up to | |
# if we're up to number 5 this inner loop will run 5 times | |
# inside of this loop have an if statement | |
# the if statement is doing a modulo calculation | |
# it would be the current number % the divider | |
# an example 5 % 2 | |
# we're checking if this calculation results in a 0 | |
# if the calculation is 0 we know that the current number isn't a prime | |
# an example, 4 % 2 equals 0 | |
# therefore we can break out of this loop | |
# we increment the divider by 1 if the result of the last if statement isn't 0 | |
# if the current number is equal to the divider we know that the number is prime and wen push it into an array | |
# an example 5 % 5 will actually run | |
# therefore we know the number is prime | |
# Q16 | |
# An allergy test produces a single numeric score which contains the information about all the allergies the person has (that they were tested for). The list of items (and their value) that were tested are: | |
# - eggs (1) | |
# - peanuts (2) | |
# - shellfish (4) | |
# - strawberries (8) | |
# - tomatoes (16) | |
# - chocolate (32) | |
# - pollen (64) | |
# - cats (128) | |
# So if Tom is allergic to peanuts and chocolate, he gets a score of 34. | |
# Write a program that, given a person’s score can tell them: | |
# a) whether or not they’re allergic to a given item | |
# b) the full list of allergies. | |
allergies = { | |
cats: 128, | |
pollen: 64, | |
chocolate: 32, | |
tomatoes: 16, | |
strawberries: 8, | |
shellfish: 4, | |
peanuts: 2, | |
eggs: 1 | |
} | |
toms_score = 34 | |
array_of_allergies = [] | |
allergies.each do |key, value| | |
if toms_score == 0 | |
break | |
end | |
if toms_score >= value | |
toms_score = toms_score - value | |
array_of_allergies << key | |
end | |
end | |
def allergic_to?(array_of_allergies, item) | |
array_of_allergies.include?(item) | |
end | |
p allergic_to?(array_of_allergies, :peanuts) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment