6 kyu

Representing Numbers on an Abacus

131 of 197scarecrw

Description:

How does an abacus work?

An abacus is a mechanical tool for counting and performing simple mathematical operations. It consists of vertical poles with beads, where the position of the beads indicates the value stored on the abacus. The abacus in the image below, for example, is storing the number 1703.

Each pole represents a decimal digit of the number, where the rightmost pole indicates the ones digit, the second to the right the tens digit, the third to the right the hundreds digit, and so on.

Each pole is split into an upper and a lower portion. Each bead in the lower portion represents a single unit for the pole (1 for the ones pole, 10 for the tens pole, 100 for the hundreds pole, etc.) The bead in the upper portion represents 5 units for the pole (5 for the ones pole, 50 for the tens pole, 500 for the hundreds pole, etc.)

For the upper portion of the pole, the bead is counted if it is in the down position. For the lower portion of the pole, a bead is counted if it is in the up position.


Task

For this kata you will need to write two functions:

  • An encoding function which will take an int as input and return a string representing that number stored on an abacus
  • A decoding function which will take as input a string representing an abacus and return the number on the abacus as an int

An abacus will be represented as a string where a '*' character represents a bead, a ' ' (space) character represents an empty spot on the pole, and a '-' character represents the dividing line between the upper and lower portions.

For example, the abacus in the image above (representing the number 1703) would be represented as:

****** **
      *  
---------
     ** *
***** ***
****** **
******** 
*********

or

'****** **\n      *  \n---------\n     ** *\n***** ***\n****** **\n******** \n*********'

Notes:

  • All abacus representations will have 9 poles
  • Input to the encoding function will be constrained to 0 <= n <= 999999999
  • Input to the decoding function will always be a valid abacus representation
Strings
Fundamentals

More By Author:

Check out these other kata created by scarecrw

Stats:

CreatedJun 26, 2021
PublishedJun 27, 2021
Warriors Trained633
Total Skips10
Total Code Submissions738
Total Times Completed197
Python Completions131
Prolog Completions5
Java Completions31
Haskell Completions9
JavaScript Completions36
Total Stars30
% of votes with a positive feedback rating96% of 69
Total "Very Satisfied" Votes64
Total "Somewhat Satisfied" Votes5
Total "Not Satisfied" Votes0
Total Rank Assessments9
Average Assessed Rank
5 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • scarecrw Avatar
  • fishenal Avatar
  • natan Avatar
  • B1ts Avatar
  • dolamroth Avatar
Ad