2,322 Reusable Code Snippet Topics

Remove Filter
Member Avatar for
Member Avatar for ddanbe

Squaring a number is a rather common task in programming, but if you want to square an integer. Easy you think! Int MySquare = AnInt *.AnInt; This will happily overflow without giving any exception if AnInt is bigger than 46340, unless you put your code in a checked block. Now …

Member Avatar for kplcjl
0
397
Member Avatar for D33wakar
0
349
Member Avatar for D33wakar

This is a simple echo server that I've written while learning sockets programming. Compile the server and run it. You can connect to it using telnet like this: telnet localhost 1337 Disconnect the client by typing "/quit" without quotes.

0
390
Member Avatar for Dani

This is a program I wrote for my x86 assembly class which generates matrices, multiplies them, and computes how long the arithmetic took. It uses Irvine32.inc which came with the textbook.

Member Avatar for ESLAMIKA
1
5K
Member Avatar for Echo89

This is a function that generates a string of random characters to the length that you specify.

Member Avatar for 5n0wn1nja
0
7K
Member Avatar for Gribouillis

This snippet prints a python list of strings in multicolumn format (similarly to linux `ls` command). It uses module prettytable, available in [pypi](https://pypi.python.org/pypi/PrettyTable).

Member Avatar for Gribouillis
3
2K
Member Avatar for Gribouillis

This snippet is reserved for users of the KDE linux desktop. It adds a *service menu* to kde applications such as dolphin and konqueror, to launch an ipython dashboard in a directory in one click. More specifically, save the text of this snippet as a file `~/.kde/share/kde4/services/ServiceMenus/opennotebook.desktop` (create the directory …

1
632
Member Avatar for ddanbe

A modal dialog is a window that you must respond to. The MessageBox for example is such a window. It is a handy tool for displaying a warning or other kind of message to the user. But, you can configure it only in a limited way. Xcelled194 did a good …

Member Avatar for ddanbe
3
974
Member Avatar for aditya369
Member Avatar for james.lu.75491856

THIS IS DANGEROOUS CODE, if it fails, it may prevent yu frome editing it. also, you can use the functions to do crazy stuff. Look at [this refrence](http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx) and change the `0x12` to what you want. (This one hold alt, so typing r will actibvate the menu that starts with …

Member Avatar for sneekula
-4
363
Member Avatar for vegaseat

Shows you how to create multiple Tkinter buttons in a loop, each with its own name, label and command response.

Member Avatar for CodingCabbage
2
1K
Member Avatar for vegaseat

The code sample shows how to get an image from the internet and show it in a Tkinter label widget.

Member Avatar for Nils_1
4
2K
Member Avatar for ddanbe

The test.txt file I'm using is: 1,2,3,Name1,Pizza,Pizza Hut,7,8,9 1,2,3,Name2,goose,my home,7,8,9 1,2,3,Name3,chicken,airplane,7,8,9 Most of the explanation can be found in the snippet. Some CSV(Comma Separated Value) files often contain as first line, a header. You could skip this, or threat it a bit differently. I had no header, so I leave …

Member Avatar for deceptikon
0
3K
Member Avatar for vegaseat
Member Avatar for ~s.o.s~

Here is a simple implementation of implementing a simple Left trim (ltrim) and Right trim(rtim) of unwanted characters from a C style string. (null terminated strings). Doesn't suffer the overheads of the memmove implementation in which the worst case scenario (a string containing all junk characters) is approx. N^2 / …

Member Avatar for Ancient Dragon
0
3K
Member Avatar for TrustyTony

Here little debugged version of my calculator posted earlier in GUI calculator thread. I prepared it after listening that their teacher did more limited calculator in around 150 lines of code with their graphics module. This is 115 lines without empty lines and comment lines [CODE]>>> li=[i for i in …

Member Avatar for fonzali
1
2K
Member Avatar for vegaseat

This example calculates timed x, y points of a projectile motion that can be used for plotting or other calculations.

Member Avatar for ddanbe
3
6K
Member Avatar for faysal.ishtiaq_1

This code snippet can be used to get the current time in 12 hour format using JS

0
2K
Member Avatar for vegaseat

A Python sort with a helper function to allow for mixed order sort like age descending and weight ascending.

3
495
Member Avatar for emil_ham

This simulator works for: 1. First Come First Serve (FCFS) 2. Round Robin (RROB) 3. Priority (PRIO) 4. Longest Job First (LJF) 5. Shortest Job First (SJF) algorithms. The program is written for gcc linux compiler. In input file a sequence of processes coming to the systems included, where also …

Member Avatar for ElGauchoUTn
1
2K
Member Avatar for ddanbe

This has by far no practical use. But as I was used to VS, handling most of the code housekeeping "behind the back", I wanted to try to make a Forms program as minimalistic as possible. Start an Empty project, include a reference to System.Windows.Forms and fill in the code. …

Member Avatar for pitic
1
169
Member Avatar for tinstaafl

Here's a simple class for what it's worth that will do simple base85 encoding and decoding. This only works with the required blocks of data and not large data structures or streams, and does no validation of the data. What it does do is encode a block of 4 8-bit …

Member Avatar for tinstaafl
0
1K
Member Avatar for seanbp

The server only does the basics. TODOs: The header method needs expanding. The file send method can't handle too large files. Maybe write custom buffer class since some buffering is done. Keep cache of recent files.

Member Avatar for JamesCherrill
0
1K
Member Avatar for vegaseat
Member Avatar for HiHe
3
4K
Member Avatar for vegaseat

Starting with Python 3.3 you now have access to a high resolution time counter (nanoseconds and better).

Member Avatar for vegaseat
6
4K
Member Avatar for Gribouillis

You can teach your linux system that executable python files must be executed with /usr/bin/python. This allows you to write python scripts without the shebang line [icode]#!/usr/bin/env python[/icode]. This snippet shows you how to do it !

Member Avatar for Gribouillis
0
1K
Member Avatar for vegaseat

Python module turtle makes it simple to draw graphics art similar to art done by the Logo language.

1
408
Member Avatar for vegaseat
Member Avatar for msteudel

I was being really lazy and didn't want to write one up myself, couldn't find one, and ended writing one up myself anyways. Hope this helps other lazy people.

Member Avatar for prafful_panwar
1
3K
Member Avatar for slate

The most efficient static sieve generator in python I have seen so far. Returns the list of primes, that are not greater than n.

Member Avatar for vegaseat
1
496
Member Avatar for ddanbe

Yes, I know Fibonacci(=sun of a good man) again. Most famous for his series, but who among you all, know that he was the man who introduced to the Western world, the Arabic numeral system(including zero) in 1202 A.D.? The Italian merchands of those days adored it. It was far …

Member Avatar for Triryche
2
580
Member Avatar for ddanbe

The luxuries we now have in the amount of pixels on a screen to draw some amazing graphs with, were lacking in the early days of computing. Most of the time you had to resort to rude printed output. It wasn’t that bad always. Sometimes it was and still is …

Member Avatar for ddanbe
1
3K
Member Avatar for ddanbe

I wanted to draw a 5 pointed star in C# and here is how I finally did it. I leave it as an exercise (some hints are given in the code) to work out how I did this. It was fun to do (but a bit hard, my trig is …

Member Avatar for ddanbe
1
4K
Member Avatar for vegaseat

A simple way to find duplicate words in a text. In this case the text is preprocessed to eliminate punctuation marks and set all words to lower case.

Member Avatar for paddy3118
2
4K
Member Avatar for Assembly Guy

**NOTE** This code won't work at the application level. It directly addresses memory at 0xB8000 which will result in a segfault at the application level. This code must be part of an OS or an application in a not-so-secure operating system like DOS, where applications can directly address any part …

Member Avatar for Assembly Guy
0
461
Member Avatar for Luckychap

Here is simple Digital Clock coded in C++ and compiler used is TurboC. A class DIGIT is designed to draw digits. Follwoing are the details for this class: Consructor: DIDGT(); for default settings DIGITE(int s,int c,int st); for user define settings s -> is size of the digit c -> …

Member Avatar for irum.nageen.3
-2
15K
Member Avatar for sandeepparekh9

Lets say you have lots of data your listview. Now you want to Group This data According to a Perticular Subitems. For Example: Suppose i have some books data in my ListView. this listview items contains Author name and Books Title. And there are 2000 Books in list view. Now …

Member Avatar for dragonfly67
1
1K
Member Avatar for Coyx

Hi all, First tutorial submission. The standardised strtotime() function really grinds my gears. I hate messing about with dates in different formats... so I mullered this little function up. It works perfectly - probably easier ways to do it, I don't know... I just include this function in my functions …

Member Avatar for diafol
1
245
Member Avatar for lxXTaCoXxl

The snippet provided will cover the basics of an on screen keyboard; the only things it doesn't have are numbers, symbols, and extra keys. This will give you the fundementals to build off of. It's written using Microsoft's XNA Framework, however it should be fairly simple to port over to …

Member Avatar for lxXTaCoXxl
0
267
Member Avatar for blamp

I have add the two functions the add and subtract I have to now add the multiply and divsion functions can someone help

Member Avatar for irum.nageen.3
-1
474
Member Avatar for vegaseat

In Las Vegas random numbers move billions of Dollars in and out of gamblers' pockets. For the mildly inquisitive here is a test of three simple generators.

Member Avatar for rubberman
1
6K
Member Avatar for RwCC

Something that we normally have to do at one point is right click on a richtextbox etc to copy/paste. Now, we all know there are keyboard shortcuts but for the end user it is better to have this feature. Here is the basic code to allow you to add this …

Member Avatar for hewllo56
0
2K
Member Avatar for kenleycapps

Ever feel the default session handling is insecure? Or do you just want some originality to show off? Well, here you are. A custom, MySQL-based session handler. It supports base64_encoding and MySQL storage. Note: I haven't tested this thing in months so I don't know how well it'll work : …

Member Avatar for pritaeas
0
456
Member Avatar for vegaseat

An example how to plot the function y = sin(x) using the WinApi function SetPixel(). The plot is centered along a line at pixel y = 200. Add an x-axis and a couple of tickmarks and it could look impressive.

Member Avatar for jack.zgx
1
4K
Member Avatar for bumsfeld

This program uses Python module re for splitting a text file into words and removing some common punctuation marks. The word:frequency dictionary is then formed using try/except. In honor of 4th of July the text analyzed is National Anthem of USA (found via Google).

Member Avatar for sujit.shakya.3
2
2K
Member Avatar for leokuz

For some reasone everytime I enter either correct mine or someone else's valid email and password, I get message: Sorry, your credentials are not valid, Please try again. What is wrong with this form?

Member Avatar for tpunt
0
411
Member Avatar for vegaseat

Just a little fun with Python's named tuple, behaves like a class but is much leaner.

Member Avatar for vegaseat
2
681
Member Avatar for vegaseat
Member Avatar for vegaseat
2
1K
Member Avatar for vegaseat

Factorials get large very rapidly. The factorial of 13 is !13 = 1*2*3*4*5*6*7*8*9*10*11*12*13 = 6227020800. This number already exceeds the unsigned long integer, and gets into the real money as the politicians say! This program uses an array of characters to store the factorial as a numeric string. Go ahead, …

Member Avatar for vegaseat
0
1K
Member Avatar for Ramy Mahrous

Here's I wrote some code to insert\retrieve images from SQL Server database 1- Create table to hold Image ID, Name and itself [CODE=SQL] USE [ImagesDatabase] GO /****** Object: Table [dbo].[Image] Script Date: 07/10/2009 23:46:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Image]( …

Member Avatar for Roberto_3
0
10K

The End.