1

I am attempting the following coding challenge.

Write a program to loop through the array named food and remove the end element of that array, and append it to the array named drink. The initial values of these arrays are:

food = ["Tempura", "Prawn", "Squid", "Fish", "Chips"]
drink = ["Kombucha", "Coconut Mango", "Allberry", "Yogurt Rice", "Milk bubble tea"]

I know how to get the last element from the first array and append it to the second array.

print drink << food.slice(-1)

I struggle with finding the loop so that I can get the following out.

food_&_drink = ["Kombucha", "Coconut Mango", "Allberry", "Yogurt Rice", "Milk bubble tea", "Chips", "Fish", "Squid", "Prawn", "Tempura"]

How to match element 1 from the food array to element 4 in the drink array. With the method that I know, I am not getting the output the want:

print food.flatten.zip(drink)
5
  • 1
    @Arend May I ask you? I get the notification that you have edited this question. I am still relatively new to stackoverflow. Is my formatting not correct when I first posted this question? What did you edit? I want to learn so that I format properly for future posts. Thanks! Commented Oct 28, 2019 at 23:21
  • Sure, I've quickly editted out the "Thanks in advance", and made the post a bit less verbose. You can see the edits I made by clicking "Edited xx time ago" above my name Commented Oct 29, 2019 at 15:20
  • Thanks Arend! I am a first year CS student, just started my first semester. I will take notes of your edit and will make it less verbose in future posts! :) Commented Oct 30, 2019 at 3:48
  • Great! Be sure to check out the wonderful help center on the asking / answering section, it contains tons of great advice on how to phrase questions. If you're a CS student I would almost consider it life lessons in there :) Commented Oct 30, 2019 at 6:38
  • Thanks Arend for telling me about this help center! I will check it out. :) Commented Oct 31, 2019 at 5:15

1 Answer 1

3

The simplest/clearest solution to reach your desired output (nondestructively):

drink + food.reverse

The solution matching your wording:

until food.empty?
  drink << food.pop
end

or even

drink << food.pop until food.empty?
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @Amadan !! I find this code very elegant `drink << food.pop until food.empty?. Thanks again.
Personally, me too — but there's people who intensely dislike statement modifiers.
Hi Amadan, May I ask you a question? When I print this code drink << food.pop until food.empty?, I get the following output. Is the any way I can hide or print out only the last line? Thanks! ["Tony", "Rhonda", "Charlie"] ["Tony", "Rhonda", "Charlie", "Bob"] ["Tony", "Rhonda", "Charlie", "Bob", "Fred"] ["Tony", "Rhonda", "Charlie", "Bob", "Fred", "Tommy"]
That line should not print anything. I don't know where that is coming from, but it's not from that line.
Thanks! I have closed VS Code and tried it again, but still printing all each line. I will try it again after I close everything and restart my computer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.