Skip to content

Instantly share code, notes, and snippets.

@rwcitek
Last active February 13, 2023 04:56
Show Gist options
  • Save rwcitek/b1515bd334c9ad640feab88c0cb33756 to your computer and use it in GitHub Desktop.
Save rwcitek/b1515bd334c9ad640feab88c0cb33756 to your computer and use it in GitHub Desktop.
Generate list of number with some tweaks

This function creates a sequential list of numbers then shuffles, removes one, and duplicates one into another list.

The challenge is to find the missing number and the duplicated number in the shuffled list.

Number list generator

number_list_generator () {
  # given an order of magnitude, set output file names
  local LC_ALL=C
  local oom=${1:-3}
  local nums=nums.1e${oom}.txt 
  local nums_shuf=${nums/txt/shuf}.txt

  # create a sequence of numbers  
  time -p seq 1 1e${oom} > ${nums}

  # shuffle, removing one and creating one duplicate ( too lazy to shuffle again or randomly pick number to duplicate )
  time -p < ${nums} shuf | sed 1d | sed 100p > ${nums_shuf}
}

One solution.

number_locator () {
  # given an order of magnitude, set input file names
  local LC_ALL=C
  local oom=${1:-3}
  local nums=nums.1e${oom}.txt 
  local nums_shuf=${nums/txt/shuf}.txt

  # locate missing and duplicate number
  time -p comm -3 ${nums} <( sort -n ${nums_shuf} ) 2> /dev/null
}



Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment