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

ray choi hw completed #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ You should see roughly the following output:

1) #say_hello returns 'hello'
Failure/Error: expect( say_hello ).to eq "hello"

expected: "hello"
got: "hi"

(compared using ==)
# ./spec/the_warmup_spec.rb:8:in `block (2 levels) in <top (required)>'

Finished in 0.0028 seconds (files took 0.08831 seconds to load)
8 examples, 8 failures

Expand All @@ -75,7 +75,7 @@ You should see roughly the following output:

#### Hooray!
* The first tests are failing. _We're seeing **RED**._
* Now we need to write code to pass the tests and _turn them **GREEN**_.
* Now we need to write code to pass the tests and _turn them **GREEN**_.
* When all the tests are green, we're ready to _stop and **REFACTOR**_!

## Warmup Challenge
Expand Down Expand Up @@ -112,7 +112,7 @@ Create a function called `user_type_prefix` that takes a level (integer) and ret
Update your `build_username` function to use prefixes. It should now accept a privilege_level (in addition to the other parameters), and tack it on to the beginning of the username (e.g. "seller-jdoe78"). See the tests for more details.

#### Level 3
Create a function called `generate_username` that has four arguments (first_name, last_name, birth_year, privilege_level) and returns a _unique_ username. Come up with a simple way to store usernames as you create them.
Create a function called `generate_username` that has four arguments (first_name, last_name, birth_year, privilege_level) and returns a _unique_ username. Come up with a simple way to store usernames as you create them.

STRETCH: If a username already exists, append "_1". Then, increment the counter each time you reuse the username (e.g. "jdoe78", "jdoe78\_1", "jdoe7\_2", "bbunny60", "bbunny60\_1").

Expand Down
145 changes: 140 additions & 5 deletions username.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,148 @@
# Run `rspec /spec/username_spec.rb` to get started.

def format_name(first, last)
nil
if (first.empty? || last.empty?)
nil
else
(first.gsub(/\s+/, '')[0]+last.gsub(/\s+/, '')).downcase
end
end

def format_year
nil
def format_year(year)
year_to_string = year.to_s
if year_to_string.length != 4
nil
else
year_to_string[2]+year_to_string[3]
end
end

def build_username
nil
#check_privilege
# returns the corresponding user type (FAILED - 2)
# defaults to the 'user' user type (FAILED - 3)
# rounds down to the closest match (FAILED - 4)
def check_privilege(i = 0)
floor_i = i.floor
if floor_i == 1
'seller'
elsif floor_i == 2
'manager'
elsif floor_i >= 3
'admin'
else
'user'
end
end

def user_type_prefix(i=0)
if (check_privilege(i) == 'user')
''
else
# for any other i append the '-'
check_privilege(i)+'-'
end
end

# adds the last two digits of the birth year to the username (FAILED - 2)
# prefixes usernames with a user_type (FAILED - 3)
# separates the user_type from the user_name with a dash (FAILED - 4)
# does not prefix level 0 usernames with 'user' (FAILED - 5)
# defaults to type 'user' if no privilege level is specified (FAILED - 6)

def build_username(first, last, year, i=0)
user_type_prefix(i)+format_name(first, last)+format_year(year)

end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this because it looks like what I was thinking.

# it appends a username count when the username already exists (FAILED - 2)
# it increments usernames that are already in use (uniqueness constraint) (FAILED - 3)
$list_of_usernames = []
def generate_username(first_name, last_name, birth_year, privilege_level=0)
user_name = build_username(first_name, last_name, birth_year, privilege_level)
# in the beginning the count is set to 0
count = 0
# while this loop is true (which is always)
while true
# and count is greater than 0
if count > 0
# set a temp user name where we are adding the "_#{counter}"
temp_user_name = user_name + "_#{count}"
else
# if the count is 0 then just set the temp_user_name to the user_name
temp_user_name = user_name
end

# once that is done, check to see if the temp user name exists in the $list_of_usernames
if $list_of_usernames.include?(temp_user_name)
count += 1
else
# if it does not then push it
$list_of_usernames.push(temp_user_name)
# and then return it and exit the loop
return temp_user_name
end
end
end

# ANSWER VIA OBJECT
#
# $user_hash = {}
# def generate_username(first_name, last_name, birth_year, privilege_level=0)
# user_name = build_username(first_name, last_name, birth_year, privilege_level)
#
# if $user_hash.key?(user_name) == false
# $user_hash[user_name] = 1
# else
# temp_user_name = user_name
# user_name = user_name + "_" + $user_hash[user_name].to_s
# $user_hash[temp_user_name] += 1
# end
# user_name
#
# end






# if user_name does not exist then push that name to the array and return that user name and
# count = 0
# if $list_of_usernames.include?(user_name)
# count = 1
#
# # ruby does not exist so you need to set it here to keep going down to the bottom
# if count > 1
# $list_of_usernames.each do |user|
# $list_of_usernames.include?(user)
# count += 1
# user_name = user_name + "_#{count}"
# end
# end
# user_name = user_name + "_#{count}"
# end

# if user_name exist set counter to 1 (initial) or ++

# then return it



#
# $list_of_usernames = []
# def generate_username(first, last, year, privilege = 0)
# created_username = build_username(first, last, year, privilege)
# if $list_of_usernames.include? created_username
# count = 1
# while $list_of_usernames.include? created_username
# if count > 1
# created_username.slice!(-1)
# created_username.slice!(-1)
# end
# created_username = created_username + "_#{count}"
# count += 1
# end
# end
# $list_of_usernames.push(created_username)
# created_username
# end
16 changes: 10 additions & 6 deletions warmup.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
# Run `rspec spec/the_warmup_spec.rb` to get started.

def say_hello
"hi"
'hello'
end

def scream(message)
message
message.upcase + '!'
end

def first_char
"z"
def first_char(word)
word[0].downcase
end

def polly_wanna
"crackercrackercracker"
def polly_wanna(word)
word*4
end

def after_you
'no, after you'
end