Having difficulty with these things is normal. There are many books and other resources with advice about this stuff. The advice varies in both quality and being a good fit for your personal context, so finding advice that works well for you can be hard. Solutions may require some personal growth by you not merely finding the right advice – you may need some teamwork between you doing some problem solving and the resources doing some problem solving, rather than just relying on one or the other.
ruby is much more flexible about omitting some parentheses than most languages.
in general, you want code to be easy to read and understand. that matters more than making it short. so don’t omit every parentheses just because you can.
regarding:
puts character_name + " is " + character_age
i don’t see what you’d want parentheses for. this looks clear enough to me. I think it’s normal to put a bunch of stuff after puts
without enclosing the inputs to puts
in parentheses.
no. i just confirmed that it works in a very old version of ruby.
Some writing:
Starbucks recently reversed its open-door policy. You now need to make a purchase to stay in the store, to use the restroom, or to get water. Overall, I think it makes sense and I’m fine with it. One thing that came to mind was how we are enforcing it. I asked for clarification on how we know someone is a paying customer. Can I ask for a receipt? Do I have to ask for a receipt? The answer I got was: no. Ok. So how do we know someone is a paying customer? Long story short: we ask.
There is no real verifying that I am supposed to. Their is nothing stopping you per policy (though this isn’t something I reviewed, I was told this and as I shared before my managers just make up stuff) to just lie and say you purchased something. You can come in, sit down, and said you bought something earlier and thats fine. I could go into more details but the point is that this just felt like something that punishes honest people. If I could do steps to verify like through receipts that would be fine, but if I am supposed to just take people in their word it sucks for those people who only bought stuff to be honest.
Uhh would it not be the first 5 things of deck? Thats what I get when I copy and paste the above code into the terminal/irb. From the documentation you shared above:
a = ['a', 'b', 'c', 'd'] a.slice!(2) # => "c" a # => ["a", "b", "d"] a.slice!(2.1) # => "d" a # => ["a", "b"]
It returns what it slices out, no? Hmm. I guess I can replace the deck[0…4] part. Is that what you were getting at? So:
cards = Array.new
hand = Array.new
deck = Array.new
suits = ["S","C","D","H"]
letters = ["A","T","J","Q","K"]
suits.each do |a|
2.upto(9) do |n|
cards << "#{n}#{a}"
end
letters.each do |l|
cards << "#{l}#{a}"
end
end
deck = cards.shuffle
hand << deck.slice!(0..4)
print hand
What would I be too reliant on? Running the code in the IDE? or?
I guess before I started using the IDE what was the expectation on how I did the coding? What I did before was write in a text file, save at as .rb, and run it through ruby using the terminal. Is there an advantage to doing that versus using the IDE? Because thats really the only future of the IDE I’m taking advantage of. Idk what else they are capable of/are meant for.
yes, you got it.
ok, next, fill in the blank.
def find_pair(hand)
# your code here
end
it should return nil
for no pair, otherwise the highest ranking pair in the hand (e.g. “A” is the best return value, then “K”, etc.)
executing code with an IDE is not an issue. there are other more advanced features.
oh that reminds me:
either don’t use AI or use it sparingly and say when you use it.
Ok. I will work on this soon. Just to confirm. Rank refers to the number part right (well, its not always numbers)? Like 2 , 3, 5, A, J, K? Pairs are things of the same rank. Not suit. Right?
The return value of find_pair(hand) should only be the highest ranking pair in the hand, right? So if there are multiple pairs it ignores the other pairs that are lower ranking.
ok
I don’t. I avoid AI like the plague. Idk if thats the right mentality to have towards it but I don’t use it.
Yes
Yes. Just return one character or nil.
You may want to write a find_all_pairs helper method and some others.
So if the hand was something like 5S, 5C, 6H, 7D, 9C it should return just 5? Not 5S and 5C?
So this is where I’ve gotten so far (worked on this for about a hour):
cards = Array.new
hand = Array.new
deck = Array.new
suits = ["S","C","D","H"]
letters = ["A","T","J","Q","K"]
suits.each do |a|
2.upto(9) do |n|
cards << "#{n}#{a}"
end
letters.each do |l|
cards << "#{l}#{a}"
end
end
deck = cards.shuffle
hand << deck.slice!(0..4)
def find_pair(hand)
hand = hand[0].map {|string| string.gsub(/[SDHC]/, '')}
end
print find_pair(hand)
My thought process was to find a way to just get the ranks from the arrays and then from there figure out how to find the pairs and stuff. I figured out how to get the rank by itself pretty fast. Most of my time was spent on trying random stuff in the documentation that I thought could’ve worked. Am I on a ok track or?
Some quick writing to share:
1.) Someone I know is involved in a lawsuit with their employer. Long story short (because I don’t know whats fine to share or not really) they had trouble finding someone to represent them. Now I don’t know how they went about looking for employment lawyers but this person is quite young (not even 20 yet). I wonder if part of the reason they had trouble finding a lawyer was because of their age. They have loads of proof (as far as I know its pretty open and shut, lots of stuff documented and what not) but had trouble finding a lawyer to talk to. The lawyer that eventually ended up taking it was surprised by the amount of documented stuff among other things for his case. They communicated to me the lawyer was shocked. Not just because of the volume but in part because they even had a case to begin with. Idk.
2.) Recently during a supervisor meeting with our store manager our district manager happened to be by for a check in and decided to join our meeting. The conversation turned into stuff about not undermining our superiors. He started talking about how we have a new policy rolling out soon about writing on every single cup. He said that when he talks to his boss, the regional manager, he’ll bring up the standard complaints about how this is going to affect speed of service, how this may be annoying, kind of fake for connecting with customers, etc., but with his inferiors he’ll discourage any bad talking about this stuff. He told us to do the same thing with our store manager. During our meetings with our store manager us supervisors are totally cool to disagree with her, say what things are stupid or bad, etc. but when we are interacting with the regular baristas we need to fully support her in whatever she’s doing. Idk. Seems kinda dishonest to me.
OK, you need to break it down into smaller parts. I’ll get you started:
def find_pair(hand)
# return rank (as 1-character string) of highest ranked pair in hand
pair_ranks = find_all_pairs(hand)
sorted_pair_ranks = pair_ranks.sort_by {|rank| rank_to_number(rank)}
return sorted_pair_ranks.last
end
def find_all_pairs(hand)
# returns an empty array for no pairs, otherwise an array with the ranks of the pairs found. cards can't be reused in multiple pairs, so 3 jacks form one pair and 4 jacks form 2 pairs.
hand_ranks = hand_to_ranks(hand)
# more code
end
def hand_to_ranks(hand)
# input is hand array with cards as 2-character strings formatted "RS" (rank then suit). Output is an array of 1-character strings formatted "R"
# code
end
def rank_to_number(rank)
# J = 11, Q = 12, K = 13, A = 14. For now we'll ignore how A can sometimes be 1.
# code
end
Do you understand how find_pair
works above?
As a general technique, you can pretend the sub-functions you need already exist, and just write out how to solve your problem using them. Then go through and create each needed sub-function afterwards.
You can run sub-functions individually and test that they’re working (although find_all_pairs
has a sub-sub-function in it, so it can’t be run all by itself). You can also frequently run one or a few lines of code from a function to test the behavior of that part by itself, or use similar code. For example:
Does this make sense and make the problem look approachable for you?
You can find complaints about employers that involve broken laws every day on r/antiwork. Some of the cases are quite serious. I doubt there exist enough employment lawyers to handle a tenth of the true complaints. Maybe this is a relevant perspective.
Yes? Uhh: it finds all the pairs, sorts them, and then returns the last number in that sorting which should be the highest value.
Ok. That makes sense. I was trying to split it up in steps but I didn’t think about needing more functions.
Yes, I think so.
Quick update:
Haven’t really worked on much the past week. Picked up a lot of double shifts the past week. Will work on taking some notes from some Giraffe Academy videos and then starting back up on the coding assignment.
Read: Paths Forward Summary. Quick read, as of this moment no thoughts to share on it… May do tomorrow.
Here’s some notes I’ve taken on Giraffe Academy’s ruby stuff:
- Working With Strings | Ruby | Tutorial 8
- Knowing how to work with strings is important. You will be working with a lot of plain text data.
- A method is a block of code we can call that will either modify or give us information about our string.
- Things put in between quotation marks are considered strings.
- use a \" to print out quotation marks
- use a \n to print out stuff on a new line
- string methods = string functions
- upcase() = string in all uppercase letters
- downcase = string in all lowercase letters
- strip = removes excess whitespace
- length = total numbers of characters in a string (spaces are included
- include? “Some_string” checks if that string is in the string being checked.
- string[ ] = to access a specific character in a string
- you can access a range of characters using string[,] for ex [0,4]. this does not include the character at 4. It will access the characters from 0,1,2,3 and not 4.
- index = where a character is in a string, you can do multiple letters and it will tell you where the string begins
- Math & Numbers - Ruby - Tutorial 9 - YouTube
- If you do puts 5 + 9 it will “put” on a new line 14. You have to put quotation marks around the sum for it to directly print out “5 + 9”.
- ** two asterisks right next to each is the same as ^ (exponents).
- % mod operator
- You can print/put a number alongside a string on the same line. However, that number has to be converted into a string. So I wouldn’t say they are exactly the same thing.
- num = 20 puts (“my fav num” + num.to_s")
- floor, ceil, abs, round - different methods for interacting with numbers in Ruby
- There are methods specific to the math class. For example, sqrt, log
- Two basic types of numbers in ruby:
- integer - whole number
- float - decimal number
- Integer interacting with an integer returns an integer
- Integer interacting with a floating point number returns a floating point number
- Floating point number interacting with a floating point number returns a floating point number.
- Getting User Input | Ruby | Tutorial 10 - YouTube
- To get user input we need to use the terminal (mac) or command prompt (windows). We can’t use the IDE.
- The command “gets” allows ruby to get user information. It stops the program until it “gets” something from the user before it continues running.
- You should store whatever your getting into a variable.
- Ruby interprets the enter you press in typing in something to gets as a new line character. To avoid this do gets.chomp().
- Building a Calculator | Ruby | Tutorial 11 - YouTube
- Whenever you enter information into Ruby, Ruby automatically converts it into a string.
- to_f to convert to float
This was over a couple of days. Sometimes I feel like I’m not posting “enough” and just hold on to stuff. Probably shouldn’t do that.
I spent 30 minutes working on the coding assignment and I’m a bit lost. Maybe its because of the break I took for the week. I’ve been just looking at stuff in the array documentation and just trying different stuff out. Should I look at other areas/documentation?
So far I only have:
def hand_to_ranks(hand)
ranks = hand[0].map {|string| string.gsub(/[SDHC]/, '')}
end
You don’t need gsub. Think about other, simpler ways to get the rank from a string like “5H”. Like brainstorm different things you could do with that which would return “5”.