DEV Community

Simon Green
Simon Green

Posted on

Two hundred slices

Weekly Challenge 200

As we reached our 200th challenge, can I just thank Mohammad for all the work each puts in to setting and maintaining the challenge each week.

Challenge, My solutions

Task 1: Arithmetic Slices

Task

You are given an array of integers.

Write a script to find out all Arithmetic Slices for the given array of integers.

An integer array is called arithmetic if it has at least 3 elements and the differences between any three consecutive elements are the same.

My solution

This one is relatively straight forward. We start with an empty list (array in Perl) called solutions.

I then iterate a variable called start from zero to 3 less than the length of the original list. For each iteration, I calculate the absolute difference between the value at that position and the value in the next position, and store this as the variable diff.

I then have an inner loop with the variable end from start + 2 to one less than the length of the list. If the the absolute difference between the value at the position end and the previous number is the same as diff, we have a solution, and add it to the solutions list. I continue until either the difference is not diff or we have reached the end of the list.

Finally, I print the solution. In the Python code, I store items in the solution list as tuples as this will expand nicely as a string. In the Perl code, I store items as a string.

Examples

$ ./ch-1.py 1 2 3 4
(1, 2, 3), (1, 2, 3, 4), (2, 3, 4)

$ ./ch-1.py 2
()
Enter fullscreen mode Exit fullscreen mode

Task 2: Seven Segment 200

Task

A seven segment display is an electronic component, usually used to display digits. The segments are labeled 'a' through 'g' as shown:

aaaa
f  b
f  b
gggg
e  c
e  c
dddd
Enter fullscreen mode Exit fullscreen mode

The encoding of each digit can thus be represented compactly as a truth table:

my @truth = qw^abcdef bc abdeg abcdg bcfg acdfg acdefg abc abcdefg abcfg^;

For example, $truth[1] = ‘bc’. The digit 1 would have segments ‘b’ and ‘c’ enabled.

Write a program that accepts any decimal number and draws that number as a horizontal sequence of ASCII seven segment displays, similar to the following:

-------  -------  -------
      |  |     |  |     |
      |  |     |  |     |
-------
|        |     |  |     |
|        |     |  |     |
-------  -------  -------
Enter fullscreen mode Exit fullscreen mode

To qualify as a seven segment display, each segment must be drawn (or not drawn) according to your @truth table.

The number "200" was of course chosen to celebrate our 200th week!

My solution

I really like these tasks, and you really need to think about the solution. The thing that makes this tricky is that multiple numbers need to appear side by side.

This is how I tacked it:

  1. Defined the list truth as set in the task.
  2. Take the input number n and turn it into a list of truths. This variables is called numbers. For example 200 is turned into ['abdeg', 'abcdef', 'abcdef']
  3. Define a list of lines. If it is a string (scalar in Perl) then it represents one of the horizontal lines. If it is a list (arrayref in Perl) then it has two values represented the two vertical lines. The first value is the left side, the second value is the right side.
  4. Iterate over each lines calling the print_row function with the value from numbers and the line.

The print_row method does the following:

  1. Define a list called row
  2. For each number:
    • If the list is a scalar, append either dashes or spaces depending if the letter from list is in the number.
    • If the list is an array, determine if the left and or right value should show a pipe | or space depending if the letter from is in the number.
  3. Concatenate the values in row by two space.

Perl doesn't have an in method like Python does. I have created a method called _in because index( $t, $s ) != -1 is a little too wordy!

Examples

$ ./ch-2.py 200
-------  -------  -------
      |  |     |  |     |
      |  |     |  |     |
-------                  
|        |     |  |     |
|        |     |  |     |
-------  -------  -------

$ ./ch-2.py 12345
         -------  -------           -------
      |        |        |  |     |  |      
      |        |        |  |     |  |      
         -------  -------  -------  -------
      |  |              |        |        |
      |  |              |        |        |
         -------  -------           -------

$ ./ch-2.py 67890
-------  -------  -------  -------  -------
|              |  |     |  |     |  |     |
|              |  |     |  |     |  |     |
-------           -------  -------         
|     |        |  |     |        |  |     |
|     |        |  |     |        |  |     |
-------           -------           -------
Enter fullscreen mode Exit fullscreen mode

Top comments (0)