18 Reusable Code Snippet Topics

Remove Filter
Member Avatar for
Member Avatar for MosaicFuneral

Haven't programmed in a long time. Last project was a GCODE pathway generator I wrote several years back. My compiler is obviously several years out of date. Uses time(), itoa(), rand(). Current standard compliant compilers should have <chrono>, <thread>, <random>, and to_string() in suppliment to those more deprecated functions. This …

Member Avatar for DGPickett
2
3K
Member Avatar for vegaseat

Generators are rather familiar objects in Python. Generators supply data on demand. Using Go you can create a generator with a goroutine and a channel. Here the goroutine is an anonymous function within a function, simple to implement.

Member Avatar for vegaseat
0
1K
Member Avatar for BustACode

I just love generators in Python. One of the cool things I found out from [this](http://nedbatchelder.com/text/iter.html) site was that one could send in a value when calling on an active generator. To do so one uses the send() method and a yield like so: "x = yield". The send with …

0
327
Member Avatar for HTMLperson5

This is a goofy sentence generator I have made in python; it took about 5 minutes to get the code to work - but its working now so if your curious try it out!

Member Avatar for HTMLperson5
-1
542
Member Avatar for TrustyTony

Here you see how one could use recursion together with generators for (inefficient) permutations generator. It is kind of interesting to know how one can do it recursively (of course there exist many ways). Anyway this snippet demonstrates how you can use recursive generator call to feed a for statement. …

0
431
Member Avatar for TrustyTony

Gribouillis has allready posted code similar to this in his snippets, but here version for input suitable for for expressions, generators and list comprehensions. Need to add robustness by some try...except blocks for real world use.

Member Avatar for vegaseat
0
535
Member Avatar for Gribouillis

This snippet defines a simple decorator to post-process a function's return value through a sequence of filters. As a first application, the decorator can transform a generator function into a function returning a sequence type (list, tuple, dict).

Member Avatar for Gribouillis
2
569
Member Avatar for TrustyTony

This is my code for anagrams not utilizing the ready prepared file of anagram synonyms to celebrate 11.11.11 11:11:11. If you start program it warns you about missing subdirectory dict and creates it. You may put any word files, one word per line, to file <dictionary name>.txt. Program lists available …

0
2K
Member Avatar for Gribouillis

Any sequence of python strings can be disguised to look like a file opened for reading, which can then be passed on the fly to functions expecting such an input file. This code snippet defines the necessary adapter class. The obtained file-like objects store only one string of the sequence …

Member Avatar for Gribouillis
2
1K
Member Avatar for TrustyTony

Here is class that can be fed directly from split stream like one number per line file (or [URL="http://www.daniweb.com/software-development/python/code/321725"]generator producing 'words'[/URL] from multiple numbers per line).

Member Avatar for Gribouillis
0
607
Member Avatar for TrustyTony

Here is simple math program to list number whose all factors are in given set of factors. I have included my timing decorator, which uses my timestring function, so I included that and imports for decorator in the decorator function, bit against the rules. Of course I could have used …

0
271
Member Avatar for TrustyTony

Another task from C++ forum is trivial in Python even taking handicap of not using Counter.

0
518
Member Avatar for TrustyTony

Little clean up of vegaseat's code for generating pi decimals. Added number of decimals desired and string type return for easy use with ''.join()

Member Avatar for TrustyTony
1
613
Member Avatar for TrustyTony
Member Avatar for TrustyTony
1
319
Member Avatar for TrustyTony

Saw somebody was viewing this thread and thought the qrange needed one update. From post [url]http://www.daniweb.com/code/snippet216627.html[/url].

Member Avatar for TrustyTony
0
588
Member Avatar for TrustyTony

This handy function turns file into stream of words stripped of punctuation, whitespace and digits, but does not split for example we'd to two words. If you want that you can further process yielded words or change the definition.

Member Avatar for griswolf
3
2K
Member Avatar for TrustyTony

See the link ([URL="http://norvig.com/sudoku.html"]Solve Every Sudoku Puzzle[/URL] for fine description of logic of the code) I attach the top95.txt file of tough problems and the sudoku claimed toughest of all time by Finnish mathematician Arto Inkala ([URL="http://www.kristanix.com/sudokuepic/worlds-hardest-sudoku.php"]The Worlds Hardest Sudoku Puzzle[/URL]) Solving this was piece of cake for the code, …

Member Avatar for TrustyTony
0
963
Member Avatar for vegaseat

This is an update of an earlier version. In this version the string containing the approximated pi value is created directly from the iteration of the generator. This way the program is simplified and can be used with Python2 and Python3 ...

1
545

The End.