Posts

Showing posts with the label python

Thoughts on Random Number Generation - QuickCheck

Image
This post was first published on 15 October 2020, but that site has since been shut down. Part 1  of this series explored pseudo-random values—statistically random values derived from a known starting point. This article explores using random values in testing. Randomness in test invocation is common; for instance,  JUnit5  provides an annotation to  randomise the order of test execution . This article, however, examines a testing style using randomly generated input values to test  properties  of code, known as "Property-Based Testing". Why use random values in testing? Defining suitable positive and negative test cases to exercise code is often difficult. Automating the execution of many randomly selected tests covers a broader range of input values. Furthermore, recording and reporting failing tests allows for replay and debugging. Property-based testing verifies program code using a large range of relevant inputs by generating a random sample of valid v...

Replacing CRLF from files

Image
The following is a number of different methods you can use to strip ^M from files. This was more of an exercise than a feature I need regularly. However, there was a recent incident at work where someone had checked-in such an abomination. Some tools just don't like Windows line endings in files, i.e. CRLF, Carriage Return Line Feed . So, as an exercise, here is a collation of the numerous ways to fix these aberrations ... Note: Most editors now have a quick way of doing this. file You can see whether a file has ^M using the file command: Example Consider a file containing ^M, file reports: $ file test.txt test.txt: ASCII text, with CRLF, LF line terminators After stripping ^M, we have: $ file test-fixed.txt test-fixed.txt: ASCII text dos2unix Probably the simplest way is to use the dos2unix command. Example Show file has ^M line endings: $ dos2unix -i test.txt 3 16 0 no_bom text test.txt Now fix: $ dos2unix test.txt Proof that file has been fixed...

Magic Triangle - Solved

Image
Magic Triangles This puzzle features in  CSIRO 's  Double Helix  blog post,  A Magic Triangle Brainteaser . The  Magic Triangle  problem involves arranging integers on a triangle. Consider a triangle with a circle at each vertex and along each side: Arrange the numbers 1 to 6 in the circles so that each side sums to the same value. This specific challenge requires each side to sum to 10. Method for Triangles First, label the nodes sequentially starting from any vertex: The solution involves the following steps: Generate all permutations of numbers 1 to 6 as  a ,  b ,  c ,  d ,  e ,  f . Filter permutations to satisfy the magic shape condition: $a + b + c = c + d e = e + f + a$. Apply the final condition:  a  +  b  +  c  = 10 . Using Haskell Generate all permutations of the numbers 1 to 6: import Data.List permutations [ 1 .. 6 ] This yields  6! = 720  permutations. Filter for sides with equal sums: [ [(a,b,c), (c,d,e), (e,f,a)] | ...