Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bus Seat Arrangement with Numbers in chart – C# Algorithm – Find Seat is W, M, S from number is inputted

W M S | S M W

01 02 03 | 19 20 21

04 05 06 | 22 23 24

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

07 08 09 | 25 26 27

10 11 12 | 28 29 30

13 14 15 | 31 32 33

16 17 18 | 34 35 36

These are the seat arrangements for a bus. I have to write a C# algorithm – When user gives a number the algorithm should return the seat is W, M or S. Example – If user enters number 26 algorithm should return M. What will the fastest way to achieve this?

>Solution :

What about this?

public static char SeatId(int seatNumber)
{
    return seatNumber >= 19 
        ? "SMW"[(seatNumber-1) % 3]
        : "WMS"[(seatNumber-1) % 3];
}

Shorter but less readable:

public static char SeatId(int seatNumber)
{
    return --seatNumber >= 18 
        ? "SMW"[seatNumber % 3]
        : "WMS"[seatNumber % 3];
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading