Weekly Challenge 135

simongreennet

Simon Green

Posted on October 23, 2021

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
💖 💪 🙅 🚩
simongreennet
Simon Green

Posted on October 23, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Weekly Challenge 142
python Weekly Challenge 142

December 12, 2021

Weekly Challenge 138
perl Weekly Challenge 138

November 9, 2021

Weekly Challenge 137
perl Weekly Challenge 137

November 1, 2021

Weekly Challenge 136
perl Weekly Challenge 136

October 31, 2021

Weekly Challenge 135
perl Weekly Challenge 135

October 23, 2021