Skip to content

Commit e92f741

Browse files
shreyans800755Kendrick Ledet
authored and
Kendrick Ledet
committed
Added BogoSort C++ Implementation (#510)
C++ Bogosort Implementation.
1 parent 00aaf38 commit e92f741

File tree

49 files changed

+2580
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2580
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ $RECYCLE.BIN/
5454
# IDE and text editors
5555
.idea/*
5656
*.swp
57+
*.iml

100_Doors_Problem/C#/Davipb/HundredDoors.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace HundredDoors
44
{
5-
public static class HundredDoors
5+
public static class HundredDoors //LOATHSOME!
66
{
77
/// <summary>
88
/// Solves the 100 Doors problem
@@ -18,7 +18,7 @@ public static bool[] Solve()
1818
for (int i = pass; i < doors.Length; i += pass)
1919
doors[i] = !doors[i];
2020

21-
return doors;
21+
return doors; //final door count
2222
}
2323
}
2424
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
#include <bitset>
3+
4+
#define DOORSNUM 100
5+
6+
void walk_and_print(std::bitset<DOORSNUM> &doors, unsigned int cycle, bool print){
7+
for(unsigned int i = 0; i < cycle;i++){
8+
for(unsigned int j = i - 1; j < DOORSNUM; j+=i){
9+
doors.flip(j);
10+
}
11+
}
12+
if(print){
13+
for(unsigned int i = 0; i < DOORSNUM; i++){
14+
if(doors[i] == 1){
15+
std::cout << "Door " << i << ": is Open"<<std::endl;
16+
}else{
17+
std::cout << "Door " << i << ": is Closed"<<std::endl;
18+
}
19+
}
20+
}
21+
}
22+
int main(){
23+
std::bitset<DOORSNUM> doors;
24+
walk_and_print(doors, 100,true);
25+
return 0;
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
-module(doors_100).
2+
-compile(export_all).
3+
-import(lists).
4+
5+
main() ->
6+
%% false represents a closed door & true represents an open door
7+
Doors = lists:duplicate(100, false),
8+
Result = toggle_doors( Doors, 0 ),
9+
io:format("Open doors:~n"),
10+
print_open_doors( Result, 1),
11+
OpenDoorsIndex = [ X*X || X <- lists:seq( 1, 10) ],
12+
TestPassed = test_open_doors( Result, 1, OpenDoorsIndex ),
13+
if TestPassed == true ->
14+
io:format("~nTest passed~n");
15+
true ->
16+
io:format("~nTest failed~n")
17+
end.
18+
19+
test_open_doors(_, 101, _) ->
20+
true;
21+
22+
test_open_doors(Result, CurrentIndex, OpenDoorsIndex) ->
23+
IsAnOpenDoor = lists:member( CurrentIndex, OpenDoorsIndex ),
24+
%% index for nth should be >=1
25+
Door = lists:nth( CurrentIndex, Result ),
26+
if IsAnOpenDoor == true ->
27+
if Door == true ->
28+
test_open_doors( Result, CurrentIndex + 1, OpenDoorsIndex );
29+
true ->
30+
io:format("test failed for ~p~n", [CurrentIndex]),
31+
false
32+
end;
33+
true ->
34+
if Door == false ->
35+
test_open_doors( Result, CurrentIndex + 1, OpenDoorsIndex );
36+
true ->
37+
io:format("test failed for ~s~n", [CurrentIndex]),
38+
false
39+
end
40+
end.
41+
42+
print_list([], _) ->
43+
io:format("~n");
44+
print_list([H|T], N) ->
45+
io:format("~p ", [H]),
46+
if (N rem 10) == 0 ->
47+
io:format("~n"),
48+
print_list(T, N+1);
49+
true ->
50+
print_list(T, N+1)
51+
end.
52+
53+
print_open_doors([], _) ->
54+
io:format("~n");
55+
print_open_doors([H|T], N) ->
56+
if H == true ->
57+
io:format("~p ",[N]),
58+
print_open_doors(T, N+1);
59+
true ->
60+
print_open_doors(T, N+1)
61+
end.
62+
63+
toggle_doors( Doors, 100 ) -> Doors;
64+
toggle_doors( Doors, Turn) ->
65+
toggle_doors( toggle_doors_per_turn( Doors, 1, Turn), Turn + 1 ).
66+
67+
toggle_doors_per_turn( Doors, Current, Turn ) ->
68+
if Current*(Turn+1) > 100 ->
69+
Doors;
70+
true ->
71+
ToggledDoors = lists:sublist( Doors, Current*(Turn + 1) - 1 ) ++
72+
[ not lists:nth( Current*(Turn + 1) , Doors) ] ++
73+
lists:sublist( Doors, Current*(Turn + 1) + 1, length( Doors ) - Current*(Turn + 1) + 1 ),
74+
toggle_doors_per_turn( ToggledDoors, Current + 1, Turn )
75+
end.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env perl
2+
3+
use strict;
4+
use warnings;
5+
6+
use POSIX;
7+
8+
sub toggle_doors {
9+
my ($doors, $start) = @_;
10+
for( my $i = 1; ($start*$i - 1) < 100; $i++ ) {
11+
$doors->[ $start*$i - 1 ] = !$doors->[ $start*$i - 1 ];
12+
}
13+
}
14+
15+
sub print_open_doors {
16+
my ($doors) = @_;
17+
print "Open doors:\n";
18+
for( my $i = 0; $i < 100; $i++ ) {
19+
if( $doors->[ $i ] ) {
20+
print ($i + 1);
21+
print " ";
22+
}
23+
}
24+
print "\n";
25+
}
26+
27+
sub test_doors {
28+
my $doors = shift;
29+
30+
my @expected = (0) x 100 ;
31+
32+
for( my $i = 1; $i <= 10; $i++ ) {
33+
$expected[ ($i*$i) - 1 ] = 1;
34+
}
35+
36+
for( my $i = 0; $i <= $#expected; $i++ ) {
37+
if( $doors->[ $i ] != $expected[ $i ] ) {
38+
die "test failed for index $i\n";
39+
}
40+
}
41+
42+
print "\nAll tests passed\n\n";
43+
}
44+
45+
my @doors = ( 0 ) x 100;
46+
for( my $i = 1; $i <= 100; $i++ ) {
47+
toggle_doors( \@doors, $i );
48+
}
49+
50+
print_open_doors( \@doors );
51+
test_doors( \@doors );
52+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
-module(harshad_number).
2+
-compile(export_all).
3+
4+
main() ->
5+
Numbers = lists:seq(1, 200),
6+
HarshadNumbers = lists:filter(
7+
fun(Number) ->
8+
Digits = integer_to_list(Number),
9+
Sum = sum( Digits, 0 ),
10+
if Number rem Sum == 0 ->
11+
true;
12+
true ->
13+
false
14+
end
15+
%% end if
16+
end
17+
%% end fun(Number)
18+
, Numbers),
19+
Expected = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
20+
12, 18, 20, 21, 24, 27, 30, 36,
21+
40, 42, 45, 48, 50, 54, 60, 63,
22+
70, 72, 80, 81, 84, 90, 100, 102,
23+
108, 110, 111, 112, 114, 117, 120,
24+
126, 132, 133, 135, 140, 144, 150,
25+
152,153, 156, 162, 171, 180, 190,
26+
192, 195, 198, 200],
27+
if HarshadNumbers == Expected ->
28+
io:format("Test passed~n");
29+
true ->
30+
io:format("Test failed~n")
31+
end.
32+
33+
sum([], Sum) ->
34+
Sum;
35+
sum([H|T], Sum) ->
36+
{Integer,_} = string:to_integer([H]),
37+
sum(T, Sum + Integer).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env perl
2+
3+
use strict;
4+
use warnings;
5+
6+
use Test::More;
7+
8+
use HarshadNumber qw(is_harshad_number);
9+
10+
my $expected = [
11+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
12+
12, 18, 20, 21, 24, 27, 30, 36,
13+
40, 42, 45, 48, 50, 54, 60, 63,
14+
70, 72, 80, 81, 84, 90, 100, 102,
15+
108, 110, 111, 112, 114, 117, 120,
16+
126, 132, 133, 135, 140, 144, 150,
17+
152,153, 156, 162, 171, 180, 190,
18+
192, 195, 198, 200
19+
];
20+
21+
my @result;
22+
for my $i( 1 .. 200 ) {
23+
if( HarshadNumber::is_harshad_number( $i ) ) {
24+
push @result, $i;
25+
}
26+
}
27+
28+
is_deeply(\@result, $expected, "Harshad numbers test passed");
29+
30+
done_testing();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env perl
2+
3+
use strict;
4+
use warnings;
5+
6+
use Test::More;
7+
8+
use HarshadNumber qw(is_harshad_number);
9+
10+
my $expected = [
11+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
12+
12, 18, 20, 21, 24, 27, 30, 36,
13+
40, 42, 45, 48, 50, 54, 60, 63,
14+
70, 72, 80, 81, 84, 90, 100, 102,
15+
108, 110, 111, 112, 114, 117, 120,
16+
126, 132, 133, 135, 140, 144, 150,
17+
152,153, 156, 162, 171, 180, 190,
18+
192, 195, 198, 200
19+
];
20+
21+
my @result;
22+
for my $i( 1 .. 200 ) {
23+
if( HarshadNumber::is_harshad_number( $i ) ) {
24+
push @result, $i;
25+
}
26+
}
27+
28+
is_deeply(\@result, $expected, "Harshad numbers test passed");
29+
30+
done_testing();

10_Harshad_Number/Ruby

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# /usr/bin/ruby
2+
3+
for i in 1..201 do
4+
sum = i.to_s.each_char.map {|c| c.to_i }.reduce(:+)
5+
6+
if i % sum == 0 then
7+
puts "#{i} is harshad"
8+
else
9+
puts "#{i} isn`t harshad"
10+
end
11+
end
12+

Ackermann/C#/xCyborg/Ackermann.cs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Ackermann
8+
{
9+
class Program
10+
{
11+
static void Main(string[] args)
12+
{
13+
Repeat:
14+
15+
Console.WriteLine("Input number A: ");
16+
long num_a = Convert.ToInt64(Console.ReadLine());
17+
18+
Console.WriteLine("Input number B: ");
19+
long num_b = Convert.ToInt64(Console.ReadLine());
20+
21+
long result = Ackermann(num_a, num_b);
22+
Console.WriteLine("A(m,n)="+result);
23+
24+
goto Repeat;
25+
}
26+
public static long Ackermann(long m, long n)
27+
{
28+
if (m == 0)
29+
{
30+
return (n + 1);
31+
}
32+
else if (m > 0 && n == 0)
33+
{
34+
return Ackermann(m - 1, 1);
35+
}
36+
else
37+
{
38+
return Ackermann(m - 1, Ackermann(m, n - 1));
39+
}
40+
}
41+
}
42+
}

Average/C++/nixfox/avg.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <iostream>
2+
#include <list>
3+
template <typename T>
4+
T average(T const* nums, int range){
5+
T sum = nums[range-1];
6+
for(unsigned int i = 0; i < (range-1); i++){
7+
sum += nums[i];
8+
}
9+
return sum / range;
10+
}
11+
int main(){
12+
// ints
13+
int nums_i[] = {1,2,3,40,5,6,7,8,100,560,2136};
14+
std::cout << average(nums_i,sizeof(nums_i)/sizeof(int))<<std::endl;
15+
// floats
16+
float nums_f[] = {20.60,45.213,23.78,90.04,29,01};
17+
std::cout << average(nums_f, sizeof(nums_f)/sizeof(float))<<std::endl;
18+
}
19+

0 commit comments

Comments
 (0)