417 Archived Topics
Remove Filter I have joined the thousands who have done it before, and have compared a number of sorting routines. The sorting is done on the same random-integer arrays. No surprises, quicksort wins this simple comparison hands down. There are clever combinations of sorting routines that are faster, like the snippet by … | |
I am sitting in front of a brandnew 27 inch Apple Macintosh running on OS X Yosemite, trying to compile a timed insertion sort from my Windows days. The Xcode app is a sweatheart, but there are some Windows things in the code that cause a problem. This line ... … | |
Just a couple of number tricks to check Go's mathematical abilities. If you have more, let's show them here. | |
A few hundred years ago the glorious leader of a big country wanted to reward the creator of the chess game. The creator of the game simply wanted one grain of rice put on the first square of the chessboard, two grains on the second, then doubling it for every … | |
The story has it that a few hundred years ago the ruler of a big country wanted to reward the creator of the chess game. The creator of the game simply wanted one grain of rice put on the first square of the chessboard, two grains on the second, then … | |
Again, calculate the minimum number of bills or coins required for a given amount of money (change). This time we will use a Go map instead of a csv string converted to a structure. | |
This Go snippet calculates the minimum number of bills or coins needed for a given amount of money (change) requested. The program also gives the denomination and number. US curency is used in this example, but can be changed to another currency. | |
Another translation of one of my Python snippets. This function will return a slice of consecutive prime numbers from 2 to a given value limit. In Go the 'int' type (int32) will go as high as 2147483647, but you can replace 'int' with 'uint64' and go as high as 18446744073709551615. | |
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. | |
Another little adventure into Go coding. This time a slice (a Go open ended array) of structures is used as a record for some data processing. For those who miss the ; at the end of a line of code, you can use them, but the lexer of the compiler … | |
Some folks go on vacation around the Easter holidays, so it would be nice to know when Easter happens in the years ahead. I had this as a Python snippet in my "oldies, but goodies" file and translated it to Go. It was actually quite easy in this case, and … | |
This time just a simple example of grading scores (0 - 100) with letters (A - F). Two approaches are presented, one using switch/case in an "on the fly" function, and the other uses the score as an index to a string of letters F through A. | |
Just some interesting applications of the bitwise and (&), or (|) operators. You might find some other uses. | |
Reading the content of a web page with a given URL is pretty simple with Go. Here we defer the closing of the response body (at an early point, so we won't later forget) until the program exits. | |
This shows the application of a Memoize decorator to speed up recursive functions in Python. | |
Use Google's modern Go language (aka. golang) to convert a denary number (base 10) to a roman numeral. Just a good exercise to explore Go's map, slice, sort and for loop features. Note that in Go the **:=** is shorthand for assigning a type (by inference) and a value to … | |
Just an example of a persistent list class that could have interesting applications in data mining. | |
Sometimes it's easier to pick an image from the internet to display in your Python Tkinter GUI toolit program. | |
Shows how to convert a comma separated value (csv) string to a list of records, and then do some data processing. The csv strings usually come from csv files created by spreadsheets. | |
Let's assume you would have to design a 500 ml (about 1.1 pint) food/beverage container like a can and use the least amount of material. Here is a Python program that explains the steps to achieve this. | |
This shows you how to create a flashcard like quiz game using a Python dictionary. It's up to you to explore the approach and make this a more meaningful game. | |
I thought it would be fun to code all the different ways to show Hello World on the display. Let's start simple ... `print("Hello World")` Can anybody print out "Hello World" vertically? | |
Just wanted to know if anyone on DaniWeb is using or at least experimenting with Go. | |
The Tkinter GUI toolkit's canvas object is very nice for all sorts of drawings and plotting. However, when it comes to saving the image Tkinter limits you to postscript printer files. One solution is to draw the same image in parallel on PIL's image-draw object in memory. This is made … | |
We have had several snippets on the format() function. This one gives a condensed overview of the many options you have. | |
A very basic pygame sprite example. Moving a sprite with the arrow keys, could be the start of a great space game. | |
Well I done it! Got bored after the holidays and bought a Raspberry Pi credit card sized computer ($35), the least expensive LED TV and a Logitech wireless Keyboard/Mouse combo. The way I ordered the kit it cost almost twice as much, but it came with a good 2.5A powersupply, … | |
One way to find the shortest distance between a series of surface (x, y) points is to let Python module itertools find the non-repeating combinations of point pairs. Now you can apply the Pythagoras' theorem to find all the distances and the minimum distance. | |
A simple code example on how to play wave sound files with the Python module PyGame. | |
Here we use the eval() function as the backbone of a simple calculator. One table is used for the display. A second table holds the buttons for the keypad. The whole thing forms an attractive looking calculator. To test it, simply paste the code into an editor like notepad and … ![]() | |
A simple way to find out a Python module's location on your drive. | |
I used csv data readily available from http://www.weatherdatadepot.com/ to create a multiline plot of the average monthly temperatures of a given location. | |
Two third party Python modules imageio and visvis make it easy to load and view images. The modules come with the pyzo scientific package or can be downloaded separately. Module imageio can read many image file formats as shown in http://imageio.readthedocs.org/en/latest/formats.html You could also replace your image file name directly … | |
With just about everybody snooping around your emails today with the excuse of hunting the bad guys, coding to keep some resemblance of privacy is getting important. I will start out with some simple encryption examples to get this started. You are invited to give us your thoughts and codes. … | |
Knowing the longitude and latitude coordinates of two cities you can calculate the distance between them. Use the code in https://www.daniweb.com/software-development/python/code/490561/postal-code-zips-and-location-python to find the coordinates. | |
Using http://download.geonames.org/export/zip/ you can get postal information for most countries in the world. The raw data string can be transformed into a Python list of lists that can be searched for zip codes, locations, longitude and latitude coordinates. | |
Using the Python win32 extensions it is relatively simple to copy to and from the clipboard. Sorry. the Windows OS is a requirement. | |
Just a small mildly optimized function to check if an integer is a prime number. For very large numbers use the Miller-Rabin primality test. There have been questions why I used `not n & 1` to check for even integer n. The more tradional` n % 2 == 0` is … | |
A dictionary in Python is an unordered set of key:value pairs. I show you how to initialize, create, load, display, search, copy and otherwise manipulate this interesting container. Like the name says, it is the closest thing to a dictionary. | |
Python's double ended queue module makes Caesar's cipher easy. | |
Use the PySide (PyQT) GUI toolkit to play animated gif files. | |
If you draw a series of close spaced shapes your eyes get fooled to see Moire patterns. Here is one example of this. | |
Just in time for the season, a star. (Playing with my grandson on the Raspberry Pi computer) | |
Once you are on the internet, you have to come up with passwords. After a while it will be difficult to remember those passwords. You could write them down somewhere, but the wrong person could find them. Why not have Python help you to come up with a logical password … | |
This snippets shows how to have fun replacing multiple words in a text. The target words and the replacement words form key:value pairs in a dictionary. The search and replacement is done using Python's regular expression module re. The code also gives an example of a function within a function. | |
An example how to use Tkinter's OptionMenu() as a combobox to allow one year to be selected from an option list of let's say the past 15 years. | |
For those of you who are inquisitive, here is a little Python program to approximate the value of pi to 77 digits. You can easily go further, just change the value n in range(n+1). | |
This snippet shows you how to animate a dice roll with the Tkinter GUI, useful for many games that require a dice to give a random number from 1 to 6. An interesting use of Tkinter's grid(), grid_forget() and after() functions. | |
Just the very basics on how to draw a rotated text using the Python GUI toolkit PySide (public PyQt). Please experiment and embellish. | |
With the advent of Python33 the module time has received a high resolution timer/counter. Here we use time.perf_counter() in a decorator to conveniently time two test functions. |
The End.