#! /usr/bin/env python # def counterfeit_detection_brute ( n, coin, correct ): #*****************************************************************************80 # ## counterfeit_detection_brute detects counterfeit coins. # # Discussion: # # We are given the weights of N coins, and the correct weight of a single # coin. We are asked to identify the counterfeit coins, that is, those # with incorrect weight. We don't know how many such coins there are. # # We simply use brute force. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 March 2019 # # Author: # # John Burkardt # # Reference: # # Kurt Bryan, Tanya Leise, # Making do with less: an introduction to compressed sensing, # SIAM Review, # Volume 55, Number 3, September 2013, pages 547-566. # # Parameters: # # Input, integer n, the number of coins. # # Input, real coin(n), the weights of the coins. # # Input, real correct, the correct weight of a single coin. # # Output, integer suspect(*), the indices of the suspected counterfeit coins. # import numpy as np suspect = np.nonzero ( coin != correct ) suspect = np.ravel ( suspect ) return suspect def counterfeit_detection_brute_test ( ): #*****************************************************************************80 # ## counterfeit_detection_brute_test tests counterfeit_detection_brute. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 March 2019 # # Author: # # John Burkardt # import numpy as np import platform print ( '' ) print ( 'counterfeit_detection_brute_test' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' Test counterfeit_detection_brute,' ) print ( ' which seeks to identify multiple counterfeit coins among n coins' ) print ( ' using brute force.' ) # # Generate the problem. # n = 100 correct = 17.0 coin = correct * np.ones ( n ) fake_num = np.random.randint ( 3, 11 ) fake_index = np.random.choice ( n, fake_num, replace = False ) fake_index.sort( ) coin[fake_index] = correct + 3.0 * np.random.normal ( fake_num ) # # Report the fakes. # print ( '' ) print ( ' There were %d fakes' % ( fake_num ) ) print ( '' ) print ( ' Indices of fakes:' ) print ( '' ) for i in range ( 0, fake_num ): print ( ' %d: %d %g' % ( i, fake_index[i], coin[fake_index[i]] ) ) # # Detect and report the fakes. # suspect = counterfeit_detection_brute ( n, coin, correct ) suspect_num = len ( suspect ) print ( '' ) print ( ' The function found %d suspects.' % ( suspect_num ) ) print ( '' ) print ( ' Indices of suspects:' ) print ( '' ) for i in range ( 0, suspect_num ): print ( ' %d: %d %g' % ( i, suspect[i], coin[suspect[i]] ) ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) counterfeit_detection_brute_test ( ) timestamp ( )