Last active
August 7, 2024 13:31
-
-
Save lopes/4498510 to your computer and use it in GitHub Desktop.
Displays the sequence until its Nth item, passed through $1. The sequence starts with 1 and its next item will be given by the read of the current item. #shell #shellscript #challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#ervilha.sh | |
# | |
# | |
# Displays the sequence until its Nth item, passed through $1. | |
# The sequence starts with 1 and its next item will be given | |
# by the read of the current item. | |
# For exemple, if the 4th item is 1211 --three one and one | |
# two--, the 5th item will be 3112. | |
# | |
# | |
# Author.: José Lopes Oliveira Jr. <indiecode.com.br> | |
# Licence: GPLv3+ | |
max=$1 # Must be an integer > 0. | |
item="1" # First sequence's item. | |
for ((count=1; count <= max; count++)); do | |
echo $item | |
aux=$item | |
item="" | |
while [[ ${#aux} -gt 0 ]]; do | |
num=${aux:0:1} # Get the first number. | |
# How many times does the number repeat in item? | |
repeat=$(echo $(($(echo $aux | sed "s/[^$num]//g" | wc -m)-1))) | |
item="$item$repeat$num" # Build next item | |
aux=${aux//$num/} # Delete all occurrences of $num in $aux. | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment