programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Bear Bibeault
• Tim Cooke
• Junilu Lacar
Sheriffs:
• Paul Clapham
• Devaka Cooray
• Knute Snortum
Saloon Keepers:
• Ron McLeod
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Frits Walraven
Bartenders:
• Carey Brown
• salvin francis
• Claude Moore

# working out empty beds exercise in python

Ranch Hand
Posts: 424
3
hi guys trying to solve this

Student rooms have 4 beds
There is a surcharge of \$8.42 per empty bed in a students’ room

straight mod wont work here will it?
im trying to get it working in an excel spreadsheet first but it isnt working

if i have 5%4 that leaves 1...but theres 3 beds that need to be added...so mod on its own doesnt help

in excel i tried
(4-MOD(number of students/4))

but this doesnt work as if the mod is 0 4-0 is 4...so its wrong
now i know i could write an IF statement for the working data in excel

the bottom line is...am i on the right path...or is there an easier way todo this?
any info/ideas apprecated

Master Rancher
Posts: 3177
119
hi Jon,

what is the question? How much rent a group of N students must pay, given the surcharge, or the number of rooms required?

To determine the number of rooms needed, why not use the '\' operator. For instance, if 11 = 2 * 5 + 1, then 11 % 5 = 1, and 11 \ 5 = 2.

If we have 1, 2, 3 or 4 students, we need 1 room. Unfortunately, students \ 4 will yield the reaults 0, 0, 0, 1. However, studens 0, 1, 2, 3 \ 4 will give 0 in all cases. That is not 1, but it is equal for all the nr of studens.
So, what do we get when we first subtract 1 from the number of students, and THEN do the '\4' operation?
We get:
nr of st.         nr of rooms (students - 1) \ 4
1, 2, 3, 4           0
5, 6, 7, 8           1
9, 10, 11, 12      2

Not quite correct, but close. Can you make it perfect?

jon ninpoja
Ranch Hand
Posts: 424
3
Just to make sure we on the same page

A room has 4 beds...so if 5 students pitch up
There is a surcharge on 3 beds / night

7 is a surcharge of 1 X 8.42 dollars (what ever it was)
Per night

You with me?

Greenhorn
Posts: 26
Jon, first a question for Piet.

Piet, did you mean to use a forward slash rather than a backslash in your reply?

Jon, one of the things that changed from legacy python (v2) to modern python was division of integers using a single slash.  Since your previous posting of code appears to be modern python, I will focus on the modern python way.

Integer division using a single slash in modern python always results in a float, not an integer.  For example, not 0.  To get an integer result use the integer divide operator "//"  The same example with the integer divide operator:

The modulo operator works the same in both versions of python.   and  However, to tie it back to your problem, the modulo operator is calculating the number of occupied beds in the room.

Does that help clarify your computation?

Travis Risner
Greenhorn
Posts: 26
Oops, Jon I goofed.

The first modulo example should be  Sorry about that.  😟

Travis Risner
Greenhorn
Posts: 26
Grrr.     whereas  Using Idle or Jupyter is an easy way to  test code snippets.  (I should have used copy and paste rather than try to type it from memory.)

Piet Souris
Master Rancher
Posts: 3177
119

Travis Risner wrote:Jon, first a question for Piet.

Piet, did you mean to use a forward slash rather than a backslash in your reply?

Ahhh, messed up here. I meant integer division (so 1 / 4 = 0), but I screwed up and I forgot that this is about Python and not Java. Thanks for correcting me.

@Jon
the idea is to determine the required number of rooms first, then you know how many beds there are, and given the number of students: voilà. But I seem to be facing a lesser period nowadays (messing up a lot lately), I leave you to the safe hands of Travis.

 Maybe he went home and went to bed. And took this tiny ad with him: Create Edit Print & Convert PDF Using Free API with Java https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire