Following my problem in the first post of Code Challenge series, it’s a solution.

## Problem

*A motor bike plate number is in the format [ AB-CD EFG.HI]. Its value is defined by: AB x C + EFG x HI, if C is a letter, its value is its ASCII code. A plate number is called NICE one if its EFG contains its HI, then its value is doubled than normal. Give you a list of place numbers, find the biggest value.*

*For example: A plate 29-C1 320.12 has value 5783 (= 29*67+320*12)*

*What is the biggest value of*

*28-A1 493.68*

*83-Y3 453.83*

*17-Z7 439.48*

*29-C1 292.29*

## Solution

It seems easy where we can compute the value of a specific plate number and find a biggest value afterward.

But, please aware that there is a trick at *if C is a letter, its value is its ASCII code. *So C can be a digit or letter. Only if it is not a digit, we need to get its value by ASCII code.

Here is my solution in C#

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
using System; namespace Solutions { internal class Problem1_PlateNumber { public static long PlateNumberValue( string plateNumber ) { string[] parts = plateNumber.Split( new char[] { '-', ' ', '.' } ); int part0 = Int16.Parse( parts[ 0 ] ); int part2 = Int16.Parse( parts[ 2 ] ); int part3 = Int16.Parse( parts[ 3 ] ); int part1 = 1; try { part1 = Int32.Parse( parts[ 1 ] ); } catch ( Exception ) { part1 = parts[ 1 ][ 0 ]; } long value = part0 * part1 + part2 * part3; if ( parts[ 2 ].Contains( parts[ 3 ] ) ) value *= 2; return value; } public static void Main( string[] args ) { string[] list = new string[] { "28-A1 493.68", "83-Y3 453.83", "17-Z7 439.48", "29-C1 292.29" }; long max = 0; string maxPlateNumber = string.Empty; foreach ( string plateNumber in list ) { long value = PlateNumberValue( plateNumber ); if ( max < value ) { max = value; maxPlateNumber = plateNumber; } } // return max } } } |

Here is the solution in Objective-C, credit to Nghia Luong to share.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
-(int)resultFromString:(NSString*) string { NSArray *components = [string componentsSeparatedByString:@" "]; NSString *firstPart = components[0]; NSString *secondPart = components[1]; NSString *ab = [firstPart componentsSeparatedByString:@"-"][0]; NSRange cRange = NSMakeRange(0, 1); NSString *c = [[firstPart componentsSeparatedByString:@"-"][1] substringWithRange:cRange]; int cRealValue = [c characterAtIndex:0]; NSString *efg = [secondPart componentsSeparatedByString:@"."][0]; NSString *hi = [secondPart componentsSeparatedByString:@"."][1]; int multiValue = 1; if ([efg rangeOfString:hi].location != NSNotFound) { multiValue = 2; } int result = ([ab intValue]*cRealValue +[efg intValue]*[hi intValue])*multiValue; return result; } -(int)bigestValueWithArray:(NSArray*)arr{ NSMutableArray *resultArr = [NSMutableArray array]; for (NSString *str in arr) { int result = [self resultFromString:str]; [resultArr addObject:[NSNumber numberWithInt:result]]; } NSArray *finalArr = [resultArr copy]; finalArr = [finalArr sortedArrayUsingSelector: @selector(compare:)]; return [[finalArr lastObject] intValue]; } // And the implement: NSArray *array = [NSArray arrayWithObjects:@"28-A1 493.68", @"83-Y3 453.83", @"17-Z7 439.48", @"29-C1 292.29", nil]; int result = [self bigestValueWithArray:array]; NSLog(@"result %d", result); |

## No code

Yes, at least we can solve it by Excel as can compute a plate number value by this function

1 |
LEFT(A2,2)*IF(ISNUMBER(MID(A2,4,1)), MID(A2,4,1), CODE(MID(A2,4,1)))+RIGHT(A2,2)*MID(A2,7,3) |

**Note: **

My full input is in attachment, not just a simple input above as it’s easy to solve manually.

[wpba-attachment-list]

391 total views, 1 views today