Last active
July 6, 2018 03:35
-
-
Save JoshCheek/71623a73bd4d1b4607dc2c5e380b5556 to your computer and use it in GitHub Desktop.
If statements (https://dev.to/joshcheek/comment/44k3)
This file contains 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
# I wrote the below code to verify the results of this video: | |
# https://www.youtube.com/watch?v=99stb2mzspI&t=399s | |
# Since you think "`if` statement is always a sign of a design flaw. Period", | |
# rewrite it without `if` statements in a way that's better (note that `unless <condition>` is `if !<condition>`). | |
require 'prime' | |
born = 2184 | |
1.upto 200 do |age| | |
year = born + age | |
bases, powers = year.prime_division.transpose | |
base = bases.reduce(:*) | |
next unless powers.uniq.size == 1 && powers[0] > 1 && base == age | |
"#{base}**#{powers[0]} = #{base**powers[0]}" | |
# => "3**7 = 2187" | |
# ,"13**3 = 2197" | |
end |
This file contains 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
# This website has a test for aspergers: https://www.wired.com/2001/12/aqtest/ | |
# The code that's supposed to score the result is apparently broken. | |
# So, I grabbed my answers and scored them with this code. | |
# Good news, I don't have aspergers! | |
# | |
# Rewrite it in a way you you think is better since you said: | |
# "`if` statement is always a sign of a design flaw. Period", | |
# “Definitely agree” or “Slightly agree” responses to questions: score 1 point. | |
agrees = [2, 4, 5, 6, 7, 9, 12, 13, 16, 18, 19, 20, 21, 22, 23, 26, 33, 35, 39, 41, 42, 43, 45, 46] | |
# “Definitely disagree” or “Slightly disagree” responses to questions: score 1 point. | |
disagrees = [1, 3, 8, 10, 11, 14, 15, 17, 24, 25, 27, 28, 29, 30, 31, 32, 34, 36, 37, 38, 40, 44, 47, 48, 49, 50] | |
{ 1=>:da,2=>:ag,3=>:ag,4=>:ag,5=>:ag,6=>:da,7=>:ag,8=>:da,9=>:da,10=>:ag,11=>:ag,12=>:ag,13=>:da, | |
14=>:ag,15=>:ag,16=>:ag,17=>:ag,18=>:ag,19=>:ag,20=>:da,21=>:ag,22=>:da,12=>:ag,13=>:da,14=>:ag, | |
15=>:ag,16=>:ag,17=>:ag,18=>:ag,19=>:ag,20=>:da,21=>:ag,22=>:da,23=>:da,24=>:ag,25=>:da,26=>:da, | |
27=>:da,28=>:ag,29=>:ag,30=>:da,31=>:da,32=>:da,33=>:ag,23=>:da,24=>:ag,25=>:da,26=>:da,27=>:da, | |
28=>:ag,29=>:ag,30=>:da,31=>:da,32=>:da,33=>:ag,34=>:ag,35=>:ag,36=>:ag,37=>:da,38=>:ag,39=>:da, | |
40=>:ag,41=>:da,42=>:da,43=>:da,44=>:ag,34=>:ag,35=>:ag,36=>:ag,37=>:da,38=>:ag,39=>:da,40=>:ag, | |
41=>:da,42=>:da,43=>:da,44=>:ag,45=>:da,46=>:da,47=>:ag,48=>:da,49=>:ag | |
}.reduce(0) { |score, (index, value)| | |
if value == :ag && agrees.include?(index) | |
score + 1 # => 2, 3, 4, 5, 7, 8, 9, 10, 11, 17, 18 | |
elsif value == :da && disagrees.include?(index) | |
score + 1 # => 1, 6, 12, 13, 14, 15, 16, 19, 20 | |
else | |
score | |
end | |
} # => 20 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment