Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! •...

62
Seattle.rb May Challenge Aja Hammerly [email protected]

Transcript of Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! •...

Page 1: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Seattle.rb !May Challenge

Aja Hammerly [email protected]

Page 2: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com
Page 3: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Who can do the most Project Euler

problems in a month?

Page 4: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Project Euler

Page 5: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

http://projecteuler.net

Page 6: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Contest Rules

• May 7th 12:00 PM - June 3rd 12:00 PM!

• Language agnostic!

• Solution must take less than a minute!

• Don't use other people's code

Page 7: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Prizes!

• $50 prize for most problems completed!

• Two divisions!

• Novice (< 1 year programming)!

• Open (everyone) !

• No prizes if < 10 people participate

Page 8: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

To participate

• Friend me!

• 73029928140835_45edcb4f778c358b11fc8589e912f0a9!

• Or send me an email of your friend code!

[email protected]

Page 9: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Why Participate?

Page 10: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Learn Ruby

Page 11: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Prepare For Interviews

Page 12: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Be Active In Seattle.rb

Page 13: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Example Problem

Page 14: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Euler #1

Page 15: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

"If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.!

!

"Find the sum of all the multiples of 3 or 5 below 1000.

Page 16: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com
Page 17: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Example Solutions

Page 18: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Solution 1sum = 0 (1...1_000).to_a.each do |n| if n % 5 == 0 then sum += n elsif n % 3 == 0 then sum += n end end p sum

Page 19: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

233168

Page 20: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Solution 2nums = (1...1000).find_all { |x| x % 5 == 0 || x % 3 == 0 } ans = nums.inject(0) { |sum, n| sum += n } p ans

Page 21: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

233168

Page 22: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Solution 3

nums = (1...1000).find_all { |x| x % 5 == 0 || x % 3 == 0 } p nums.inject(&:+)

Page 23: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

233168

Page 24: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Solution 4

threes = (1..1000).select {|x| x%3 == 0} fives = (1..1000).select {|x| x%5 == 0} fifteens = (1..1000).select {|x| x%15 == 0} (threes + fives - fifteens).inject(:+)

Page 25: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

233168

Page 26: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Solution 5

p 1000.times.find_all{ |x| x % 5 == 0 || x % 3 == 0 }.inject(&:+)

Page 27: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

233168

Page 28: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Some Useful Ruby

Page 29: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Numbery Stuff

Page 30: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

%

Page 31: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

> 5 % 2 1 !

!

> 321 % 3 0

Page 32: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

**

Page 33: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

> 5 ** 2 25 !

> 3 ** 3 27

Page 34: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

^

Page 35: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

> 5 ^ 2 7 !

> 3 ^ 3 0

Page 36: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Prime

Page 37: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

times

Page 38: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

_

Page 39: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

> 1_000 > 1000 !

> 2_000_000_000 > 2000000000

Page 40: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Stringy Stuff

Page 41: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Reverse

Page 42: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

> "Hello".reverse "olleH"

Page 43: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

split

Page 44: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

> "Hello".split('') ["H", "e", "l", "l", "o"] !

> "Apple,Pear,Banana".split(',') ["Apples", "Pear", "Banana"]

Page 45: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Enumerable

Page 46: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

find_all

Page 47: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

> (1..10).find_all { |x| x < 5 } [1, 2, 3, 4]

Page 48: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

inject

Page 49: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

> (0..10).inject {|s, n| s += n} 55 !

> (0..10).inject :+ 55

Page 50: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

downto upto

Page 51: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

> 3.downto(1) { |x| puts x } 3 2 1 !

> 3.upto(6) { |x| puts x } 3 4 5 6

Page 52: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

each_cons

Page 53: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

>(1..5).each_cons(3) { |a| p a } [1, 2, 3] [2, 3, 4] [3, 4, 5]

Page 54: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

map

Page 55: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

> [1, 2, 3].map { |x| x * 10 } [10, 20, 30]

Page 56: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Other stuff

Page 57: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Ranges

Page 58: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

> (1..3).max 3 !

> (1...3).max 2

Page 59: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Re-opening Classes

Page 60: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

Timing

Page 61: Seattle.rb May Challenge - thagomizer.com · To participate • Friend me! • 73029928140835_45edcb4f778c358b 11fc8589e912f0a9! • Or send me an email of your friend code! • aja.hammerly@gmail.com

$ time ruby my_script.rb !

real 0m0.118s user 0m0.062s sys 0m0.056s