W M S | S M W
01 02 03 | 19 20 21
04 05 06 | 22 23 24
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];
}