Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
chr in Python
#1
Question: Write a program in Python to allocate and display Block and Floor No. on the basis of Customer Number. Assuming there are 10 Blocks ('A' to 'J') with 5 floors (0 to 4) each and allocated to customers sequentially as per their Customer Number. For example: Customef no 1 gets [Block A Floor 0], Customer no 3 gets [Block A Floor 2], Customer no 7 gets [Block B Floor 1]

NOTE: This program has to be done only using arithmetic operations and type conversions (i.e., without use of if-else)

HINT: ASCII codes for 'A','B'.. are 65, 66,... & chr(65) in python is 'A'

I am new to python and this forum so I am extremely sorry if I broke any rules Big Grin

I have tried everything I can (at this stage) but I can't seem to find the logic and I am also not very familiar with chr function.

Please help me.
Reply
#2
If you 'have tried everything' then maybe you can share your results with us.

Using old-school paper and pencil can help to understand logic (customer number in parentheses)

A 0 1 2 3 4 -> (1)A0, (2)A1, (3)A2, (4)A3, (5)A4
B 0 1 2 3 4 -> (6)B0, (7)B1, (8)B2, (9)B3, (10)B4
/../

I think you can spot the pattern. You should ask yourself: what should I do to get from 7 (customer number) B1 (block and floor).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
The customer gets a number that will correspond to the letter of their block. If they are in block A there number is 65, then if they have floor number 3, their number could be - 653 - chr(65), 3 - A, 3.
Reply
#4
(May-14-2019, 01:18 PM)perfringo Wrote: If you 'have tried everything' then maybe you can share your results with us.

Using old-school paper and pencil can help to understand logic (customer number in parentheses)

A 0 1 2 3 4 -> (1)A0, (2)A1, (3)A2, (4)A3, (5)A4
B 0 1 2 3 4 -> (6)B0, (7)B1, (8)B2, (9)B3, (10)B4
/../

I think you can spot the pattern. You should ask yourself: what should I do to get from 7 (customer number) B1 (block and floor).

I thought that I will have a variable set to 64.
Then from 1-5 I will get 1 and add it to 64 to get 65(A)
Then from 6-10 I will get 2 and add...

But I am getting 1 from 1-4 and 2 from 5-9.
Multiples of 5 are causing problems. I have tried fllor division, remainders.. but nome of it works.
Reply
#5
(May-14-2019, 01:34 PM)DarkCraftPlayz Wrote: Multiples of 5 are causing problems. I have tried fllor division, remainders.. but nome of it works.

How 'none of it works'? 7 divided by 5 gives 1 and 2 as reminder. I can spot that this is exactly what is needed (hint: first is zero-based index second is finger-based index)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
Ascii table shows how numbers are stored in the memory

[Image: asciifull.gif]

chr(number) function allows you to convert number into character. Here's example:

hello_world_numbers = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
print("".join(chr(n) for n in hello_world_numbers))
Output:
hello world
If that doesn't make much sense you can take print 1 chr() at once and get similar result like:
print(   
    chr(104),
    chr(101),
    chr(108),
    chr(108),
    chr(111),
    chr(32),
    chr(119),
    chr(111),
    chr(114),
    chr(108),
    chr(100)
    )
Output:
h e l l o w o r l d
To get the block number you can use division, math.floor function and add 65 as suggested in previous post, to get the floor number you can use "modulo" operation.

Here's how math.floor can be used:

from math import floor
print(floor(10.21312312))
Output:
10
Reply
#7
(May-14-2019, 01:40 PM)perfringo Wrote:
(May-14-2019, 01:34 PM)DarkCraftPlayz Wrote: Multiples of 5 are causing problems. I have tried fllor division, remainders.. but nome of it works.

How 'none of it works'? 7 divided by 5 gives 1 and 2 as reminder. I can spot that this is exactly what is needed (hint: first is zero-based index second is finger-based index)

Sorry but I still dont understand. When I divide 1-4 with 5, it gives 0 as quotient but when I divide 5 with 5 it gives 1, which goes in the next group. Basically, I am having problem with all the multiples of 5. I need their output to be in the previous group hut they are going in the next group.
Reply
#8
Following is not using chr(), so I am not doing your homework :-), but this one way of handling problem of 'multiples of 5':

>>> blocks = 'ABCDEFGHIJ'
>>> floors = '01234'
>>> for customer in [0, 3, 5, 7]:
...     block, floor = divmod(max(customer - 1, 0), 5)
...     print(f'Block {blocks[block]} Floor {floors[floor]}')
... 
Block A Floor 0
Block A Floor 2
Block A Floor 4
Block B Floor 1
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
(May-14-2019, 08:35 PM)perfringo Wrote: Following is not using chr(), so I am not doing your homework :-), but this one way of handling problem of 'multiples of 5':

>>> blocks = 'ABCDEFGHIJ'
>>> floors = '01234'
>>> for customer in [0, 3, 5, 7]:
...     block, floor = divmod(max(customer - 1, 0), 5)
...     print(f'Block {blocks[block]} Floor {floors[floor]}')
... 
Block A Floor 0
Block A Floor 2
Block A Floor 4
Block B Floor 1

Wow, that code gives me some ideas about what I need to learn next on Python.

But I think I finally figured out the logic.
It will be:

a = int(input())
b = a - 1
print (chr((b//5) + 64))
I havent tried it in a computer yet but I think it will work.

Thanks you guys for all your help :D
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020