DEV Community

Simon Green
Simon Green

Posted on

Weekly Challenge 135

Challenge, My solutions

TASK #1 › Middle 3-digits

Task

You are given an integer.

Write a script find out the middle 3-digits of the given integer, if possible otherwise throw sensible error.

My solution

This task was pretty straight forward.

  • Check the an integer was provided.
  • If it is negative, make it positive with the abs function.
  • Display an error if it contains an even number of digits, or one digit
  • The first character we want to show can be calculated by (length - 3) ÷ 2. Use this value with substr to display the middle three digit

Examples

$ ./ch-1.pl 1234567
345

$ ./ch-1.pl -123
123

$ ./ch-1.pl 1
too short

$ ./ch-1.pl 10
even numbers of digits
Enter fullscreen mode Exit fullscreen mode

TASK #2 › Validate SEDOL

Task

You are given 7-characters alphanumeric SEDOL.

Write a script to validate the given SEDOL. Print 1 if it is a valid SEDOL otherwise 0.

My solution

The first thing that I need to do is check if the input is valid. I use the regular expression ^[B-DF-HJ-NP-TV-Z0-9]{6}[0-9]$ to perform this. According to the specification, vowels are not valid. Did anyone else sing the alphabet song when generating the regular expression? :)

I set the value of $sum to the last digit. I then go through the other digits. I work out the value of the digit in the position (0-9 if it is is a number, 10-35 if it is a letter) and multiple the weight.

If the resulting sum is divisible by 10, I return 1 (a valid SEDOL value), otherwise I return 0.

Examples

$ ./ch-2.pl 2936921
1

$ ./ch-2.pl 1234567
0

$ ./ch-2.pl B0YBKL9
1
Enter fullscreen mode Exit fullscreen mode

Top comments (0)