The Weekly Challenge 052

Stepping Numbers and Pick Coins Game

Solutions on GitHub

Task 1: Stepping Numbers

Write a script to accept two numbers between 100 and 999. It should then print all Stepping Numbers between them. A stepping number is a number in which all adjacent digits have an absolute difference of 1.

Example Output

Input: 100 150 Output: 101, 121, 123, 132, 134, 143, 145

Logic

Iterate through the range. For each number, split it into digits and verify that the absolute difference between each adjacent pair of digits is exactly 1.

Perl Solution

ch-1.pl

sub is_stepping { my $number = shift; my $seq; for my $num (split(//, $number)) { unless (defined $seq) { $seq = $num; next; } return 0 if abs($num - $seq) != 1; $seq = $num; } return 1; }

Python Solution

ch-1.py

def is_stepping(number: str): number = str(number) seq = str(number)[0] for i in number[1:]: if abs(int(i)-int(seq)) != 1: return 0 seq = i return 1

Task 2: Pick Coins Game

Suppose there are coins arranged on a table in a line. A player can only pick one coin at a time from either end. The goal is to obtain the larger total amount against the computer.

Logic

Simulate the game with a deque (double-ended queue). In each round, the player decides whether to pop from the left or right, while the computer chooses randomly or strategically from the ends, updating their respective scores.

Perl Solution

ch-2.pl

my @deck = shuffle('£1', '50p', '1p', '10p', '5p', '20p', '£2', '2p'); my %values = ('£1'=>1, '50p'=>0.5, '1p'=>0.01, '10p'=>0.1, '5p'=>0.05, '20p'=>0.2, '£2'=>2, '2p'=>0.02); sub do_draw { my $side = shift; return pop @deck if $side eq 'R'; return shift @deck; }

Python Solution

ch-2.py

from collections import deque class Lucky_Winner: def __init__(self): self.deck = deque(['£1', '50p', '1p', '10p', '5p', '20p', '£2', '2p']) shuffle(self.deck) self.values = {'£1': 1, '50p': 0.5, '1p': 0.01, '10p': 0.1, '5p': 0.05, '20p': 0.2, '£2': 2, '2p': 0.02} def do_draw(self, side: str): if side == 'L': return self.deck.popleft() return self.deck.pop()