Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ruby week 1 #485

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
d0ad277
Chapter 9. excercise 1: ASK
tadasmajeris Sep 7, 2016
f3ef8bf
Chapter 9. Old school Romans
tadasmajeris Sep 7, 2016
2f6d484
Chapter 9. Roman numbers. complex refactoring :)
tadasmajeris Sep 7, 2016
7008ee3
Chapter 10. Shuffle method
tadasmajeris Sep 8, 2016
a87d1c7
Chapter 10. Dictionary sort
tadasmajeris Sep 8, 2016
b969da9
Chapter 10. quindecillion...
tadasmajeris Sep 8, 2016
0071f24
Chapter 10. 99 bottles of beer...
tadasmajeris Sep 8, 2016
ad957b1
Chapter 10. recursive sort
tadasmajeris Sep 8, 2016
5ac3e32
Chapter 11. safer picture downloading
tadasmajeris Sep 9, 2016
2f852a8
Chapter 11. Build my own playlist
tadasmajeris Sep 9, 2016
ce0971c
Chapter 11. FIX: build your own playlist now searches the subdirector…
tadasmajeris Sep 9, 2016
5366fc8
Small typo from last fix
tadasmajeris Sep 9, 2016
24e2a94
Chapter 11. Build better playlist &&& much nicer to read code! Yours …
tadasmajeris Sep 9, 2016
ba6fbbb
Chapter 12. One billion seconds is soon..
tadasmajeris Sep 10, 2016
9ac4188
Chapter 12. Happy BDAY, SPANK
tadasmajeris Sep 10, 2016
ca714d2
Chapter 12. Party like its roman integerrr
tadasmajeris Sep 11, 2016
27128c4
Chapter 12. Birthday helper. John and Yoko...
tadasmajeris Sep 11, 2016
5e6ce20
Chapter 13. Extended Integer class
tadasmajeris Sep 11, 2016
1669125
Chapter 13. Interactive baby...
tadasmajeris Sep 11, 2016
ff02e96
Chapter 13. Orange treeeee
tadasmajeris Sep 11, 2016
3b22f01
Chapter 14. Even better profiling
tadasmajeris Sep 12, 2016
8b5b322
Chapter 14. Grandpa clock
tadasmajeris Sep 12, 2016
56cd5b6
Chapter 14. Program logger
tadasmajeris Sep 12, 2016
654f1b6
Chapter 14. Better program loggerrr
tadasmajeris Sep 12, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions ch09-writing-your-own-methods/ask.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
def ask question
# your code here
def ask question

while true
puts question
reply = gets.chomp.downcase

if (reply == 'yes' || reply == 'no')
return reply == 'yes'
else
puts 'Please answer "yes" or "no".'
end
end
end
16 changes: 15 additions & 1 deletion ch09-writing-your-own-methods/old_school_roman_numerals.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
def old_roman_numeral num
# your code here
romans = ['I', 'V', 'X', 'L', 'C', 'D', 'M']
values = [ 1, 5, 10, 50, 100, 500, 1000 ]
result = ''

values.select!{ |v| v <= num }

(values.length-1).downto(0) { |i|
max_num = values[i]
(num / max_num).times {
result += romans[i]
num -= max_num
}
}

return result
end
19 changes: 18 additions & 1 deletion ch09-writing-your-own-methods/roman_numerals.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
def roman_numeral num
# your code here
roman_letters = "IVXLCDM"
mods = {:C => 100, :X => 10, :I => 1}

roman = 'M' * (num / 1000)

mods.each do |k, v|
digit = num % (10 * v) / v
id = roman_letters.index(k.to_s)

if (digit == 9) || (digit == 4)
roman += k.to_s + (digit == 9 ? roman_letters[id+2] : roman_letters[id+1])
else
roman += roman_letters[id+1] * (num % (10 * v) / (5 * v))
roman += roman_letters[id] * (num % (5 * v) / v)
end
end

roman
end
21 changes: 20 additions & 1 deletion ch10-nothing-new/dictionary_sort.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
def dictionary_sort arr
# your code here
recursive_sort arr, []
end

def recursive_sort unsorted_array, sorted_array
smallest_id = 0

(unsorted_array.length-1).times { |i|
if unsorted_array[smallest_id].downcase > unsorted_array[i+1].downcase
smallest_id = i+1
end
}

sorted_array.push(unsorted_array[smallest_id])
unsorted_array.delete_at(smallest_id)

if unsorted_array.length > 0
recursive_sort unsorted_array, sorted_array
else
return sorted_array
end
end
69 changes: 67 additions & 2 deletions ch10-nothing-new/english_number.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,68 @@
def english_number number
# your code here
end
if number < 0
return 'Please enter a number that isn\'t negative.'
elsif number == 0
return 'zero'
end

num_string = ''

ones_place = ['one', 'two', 'three',
'four', 'five', 'six',
'seven', 'eight', 'nine']
tens_place = ['ten', 'twenty', 'thirty',
'forty', 'fifty', 'sixty',
'seventy', 'eighty', 'ninety']
teenagers = ['eleven', 'twelve', 'thirteen',
'fourteen', 'fifteen', 'sixteen',
'seventeen', 'eighteen', 'nineteen']

big_numbers = { :quindecillion => 10**48, :quattuordecillion => 10**45, :tredecillion => 10**42,
:duodecillion => 10**39, :undecillion => 10**36, :decillion => 10**33,
:nonillion => 10**30, :octillion => 10**27, :septillion => 10**24,
:sextillion => 10**21, :quintillion => 10**18, :quadrillion => 10**15,
:trillion => 10**12, :billion => 10**9, :million => 1_000_000,
:thousand => 1000, :hundred => 100 }

left = number

big_numbers.each do |word, num|
write = left / num
left = left - write * num

if write > 0
numbers = english_number write
num_string = num_string + numbers + " #{word}"

if left > 0
num_string = num_string + ' '
end
end
end

write = left / 10 # How many tens left?
left = left - write * 10 # Subtract off those tens.

if write > 0
if ((write == 1) and (left > 0))
# Since we can't write "tenty-two" instead of "twelve"
num_string = num_string + teenagers[left-1]
left = 0
else
num_string = num_string + tens_place[write-1]
end

if left > 0
num_string = num_string + '-' # So we don't write 'sixtyfour'...
end
end

write = left # How many ones left to write out?
left = 0 # Subtract off those ones.

if write > 0
num_string = num_string + ones_place[write-1]
end

num_string
end
12 changes: 11 additions & 1 deletion ch10-nothing-new/ninety_nine_bottles_of_beer.rb
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
# your code here
load 'english_number.rb'

num_at_start = 21 # change to 9999 if you want

num_at_start.downto(1) do |num_now|
puts english_number(num_now).capitalize + " bottle#{num_now==1 ? '' : 's'} of beer on the wall, " +
english_number(num_now) + " bottle#{num_now==1 ? '' : 's'} of beer!"

puts "Take one down, pass it around, " +
(num_now == 1 ? 'no more' : english_number(num_now-1)) + " bottle#{num_now == 2 ? '' : 's'} of beer on the wall!"
end
4 changes: 3 additions & 1 deletion ch10-nothing-new/shuffle.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
def shuffle arr
# your code here
arr.shuffle!
half = arr.length / 2
arr = arr.slice(0, half).shuffle + arr.slice(half, arr.length).shuffle
end
18 changes: 17 additions & 1 deletion ch10-nothing-new/sort.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
def sort arr
# your code here
recursive_sort arr, []
end

def recursive_sort unsorted_array, sorted_array
smallest_id = 0

(unsorted_array.length-1).times { |i|
smallest_id = i+1 if unsorted_array[smallest_id] > unsorted_array[i+1]
}

sorted_array.push(unsorted_array.delete_at(smallest_id))

if unsorted_array.length > 0
recursive_sort unsorted_array, sorted_array
else
return sorted_array
end
end
93 changes: 92 additions & 1 deletion ch11-reading-and-writing/build_a_better_playlist.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,94 @@
# This program finds all music files (.mp3, .wav, .aif)
# in your chosen directory - 'MUSIC_PATH',
# and makes a SHUFFLED .m3u playlist.
def shuffle arr
arr.shuffle!
half = arr.length / 2
arr = arr.slice(0, half).shuffle + arr.slice(half, arr.length).shuffle
end

def music_shuffle filenames
# your code here
filenames_arr = []
files_by_dir = {}

filenames.each do |name|
dir_name = name.split('/')[0...-1].join('/') #[0...-1] all but last element (/filename.xxx)

# create a hash, where key is directory name, and value is array of files in that dir..
files_by_dir[dir_name] ||= []
files_by_dir[dir_name] << name
end

# shuffle files in each directory
files_by_dir.each { |dir, arr| arr = shuffle(arr) }

new_list = []
filenames.length.times do |i|
# take one item from each shuffled directory
files_by_dir.each do |dir, arr|
if !arr.empty?
# and add it to the new list // first or last item for more variety
new_list.push( (i%2==0) ? arr.shift : arr.pop )
else
files_by_dir[dir].clear
end
end
end

new_list
end

def get_playlist_name
puts 'What would you like to call this playlist?'
playlist_name = ''
until playlist_name != ''
playlist_name = gets.chomp.strip
puts 'Please, enter a name' if playlist_name == ''
end
playlist_name = playlist_name + '.m3u'
end

def get_file_names path
file_names = Dir['./**/*.{mp3,wav,aif,aiff,m4a}']

if file_names.length == 0
puts "ERROR! Directory '#{path}' is empty!"
exit
end
file_names.sort! # sort alphabetically, as it first adds .mp3, then .wav and so on..
end

def change_dir_to path
if Dir[path].empty?
puts "ERROR! Directory '#{path}' not found!"
exit
end
Dir.chdir(path)
end

def make_playlist(playlist_name, files)
playlist_text = ""
files.each { |name| playlist_text << name + "\n" }

File.open playlist_name, 'w' do |f|
f.write playlist_text
end

puts
puts 'Done!'
end

#--------------------------- PROGRAM ---------------------------#
MUSIC_PATH = '/Users/tadasmajeris/Music/GROSIU/- deep : house -'

# Find if given path exists, change working directory to it
change_dir_to MUSIC_PATH

# Find all of the tracks to be moved and shuffle them
file_names = music_shuffle(get_file_names MUSIC_PATH)

# Get user to enter the playlist name
playlist_name = get_playlist_name

# Make the playlist, save it to .m3u
make_playlist(playlist_name, file_names)
40 changes: 39 additions & 1 deletion ch11-reading-and-writing/build_your_own_playlist.rb
Original file line number Diff line number Diff line change
@@ -1 +1,39 @@
# your code here
# This program finds all music files (.mp3, .wav, .aif)
# in your chosen directory - 'MUSIC_PATH',
# and makes a .m3u playlist from it.
# Tracks are sorted alphabetically

MUSIC_PATH = '/Users/tadasmajeris/Downloads/nu electro'

if Dir[MUSIC_PATH].empty?
puts "ERROR! Directory '#{MUSIC_PATH}' not found!"
exit
end
Dir.chdir(MUSIC_PATH)

# First we find all of the tracks to be moved.
file_names = Dir['./**/*.{mp3,wav,aif}']

if file_names.length == 0
puts "ERROR! Directory '#{MUSIC_PATH}' is empty!"
exit
end
file_names.sort! # sort alphabetically, as it first finds .mp3, then .wav

puts 'What would you like to call this playlist?'
playlist_name = ''
until playlist_name != ''
playlist_name = gets.chomp.strip
puts 'Please, enter a name' if playlist_name == ''
end
playlist_name += '.m3u'

playlist_text = ""
file_names.each { |name| playlist_text << name + "\n" }

File.open playlist_name, 'w' do |f|
f.write playlist_text
end

puts
puts 'Done!'
39 changes: 38 additions & 1 deletion ch11-reading-and-writing/safer_picture_downloading.rb
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
# your code here
# This program safely copies all .jpg from current directory to a chosen image_path
image_path = './PictureInbox'
if Dir[image_path].empty?
Dir.mkdir(image_path)
end
Dir.chdir(image_path)

# First we find all of the pictures to be moved.
pic_names = Dir['../*.jpg']

puts 'What would you like to call this batch?'
batch_name = gets.chomp

puts
print "Downloading #{pic_names.length} files: "

pic_number = 1
pic_names.each do |name|
print '.' # This is our "progress bar".
new_name = if pic_number < 10
"#{batch_name}0#{pic_number}.jpg"
else
"#{batch_name}#{pic_number}.jpg"
end


if !File.exist?(new_name)
File.rename name, new_name
pic_number = pic_number + 1
else
puts "\n\n"
puts "ERROR! File '#{new_name}' already exists in #{Dir.pwd}"
exit
end
end

puts
puts 'Done!'
4 changes: 4 additions & 0 deletions ch12-new-classes-of-objects/bdays.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Alice Winters, Jan 5, 1989
Tadas Majeris, May 21, 1986
John Lennon, Oct 9, 1940
Yoko Ono, Feb 18, 1933
Loading