Tag Archive for generator

Wrap tab-delimited strings in HTML

#! /usr/bin/perl -w
use strict;

#Time-stamp: <2005-01-12 11:30:05 NSussman>

############################################################
#                                                          #
#                                                          #
#                                                          #
#                      NOAH SUSSMAN                        #
#                                                          #
#                  Tabs To Targets                         #
#                                                          #
#                Created Jan 12 2005 at 1:10am             #
#                                                          #
#   Given tab-delimited data, place each line into a       #
#   heredoc and print to STDOUT then go on to the          #
#   next line                                              #
#                                                          #
#   Ignore #commented lines and blank lines.               #
#                                                          #
#                                                          #
############################################################

my $input_file= shift;    #file containing tab-delimited data

open(TABBED_DATA_HANDLE, $input_file) || die("Couldn't open $input_file
");

chomp(my @whole_file=<TABBED_DATA_HANDLE>);

foreach my $one_line (@whole_file) {
next if ($one_line =~ m/^s*$/);    #ignore blank lines
next if ($one_line =~ m/^#/);       #ignore lines starting with "#"

my @record = split(/	/, $one_line);

print "
";

#Place targets in the heredoc.
print <<CODEFRAGMENT;
<!--////////////////$record[2] block of html////////////////////////////////////////////////////////////////-->

<tr>
<td class="smelly">$record[1]</td>
<td class="dogfood">$record[0]</td>
<td class="teaParty">$record[2]</td>
</tr>

<!--////////////////end of $record[2] block of html////////////////////////////////////////////////////////////////-->
CODEFRAGMENT
#end of heredoc.

print "
";
}

=item
Here is an example data file:

big	fuzzy	bear
little	orange	kangaroo
large	grumpy	koala
small	sleepy	panda

=cut

source

Python – Copy List

a = [1, 2, 3]
b = a[:]

source

Java prime number generator

package com.destiney.Prime;
class Prime
{
public static void main( String args[] )
{
int x, y, c = 0;
for( x = 2; x < 1000; x++ )
{
if( x % 2 != 0 || x == 2 )
{
for( y = 2; y <= x / 2; y++ )
{
if( x % y == 0 )
{
break;
}
}

if( y > x / 2 )
{
System.out.println( x );
c++;
}
}
}
System.out.println( "
Total: " + c );
}
}

source

C++ random number generator

#include <iostream>
#include <cstdlib>
#include <ctime>

int main(int argc, char *argv[]){
if(argc != 3){
cout << "Usage: " << endl;
cout << "       rand [int x] [int y]" << endl;
cout << "       [x] = highest possible value of a random number" << endl;
cout << "       [y] = quantity of random numbers created" << endl << endl;
exit(1);
}
long int M = atoi(argv[1]);
int quantity = atoi(argv[2]);
srand(time(NULL));
for(int count = 1; count <= quantity; ++count)
cout << (((int)(((double)rand()/(double)(RAND_MAX+1))*M))*-1)+1 << endl;
return 0;
}

source

C++ prime number generator

#include <iostream>
using namespace std;

int main()
{
int i, j;

for ( i = 2; i < 100000; i++ )
{
for ( j = 2; j <= i/2; j++ )
{
if ( ! ( i % j ) ) break;
}

if ( j > i / 2 ) cout << i << endl;
}

return 0;
}

source

Pi

def pi():
# Compute digits of Pi.
# Algorithm due to LGLT Meertens.
k, a, b, a1, b1 = 2, 4, 1, 12, 4
while 1:
p, q, k = k*k, 2*k+1, k+1
a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
d, d1 = a//b, a1//b1
while d == d1:
yield d
a, a1 = 10*(a%b), 10*(a1%b1)
d, d1 = a//b, a1//b1

source