What I'm doing here is probably stupid and useless, but it turned out to be a great way to learn php, I appreciate your help.
I came up with a cipher which wraps text in a spiral much like Ulam's spiral maps numbers.
Refer to this image:
I consider the whole spiral as a cube, so if the string is too short to form a full cube, the remaining characters are left as empty spaces (in my picture example there is one space left, because the string is 24 characters and the next full cube is at 25 characters.
I want to add a further step of obfuscation, reading the array diagonally, so the output would be like this:
What is an easy/efficient way to achieve this? I'm storing the data in a 2D array, so it looks like this:
field[0][0]='l';
Bonus tangent question: How easily would something like this be deciphered?
Thank you!
This is the schema for a 5x5 square:
yx yx yx yx yx
0 1 2 3 4 a > 00
0 a b c d e b > 10 01
1 b c d e f c > 20 11 02
2 c d e f g d > 30 21 12 03
3 d e f g h e > 40 31 22 13 04
4 e f g h i f > 41 32 23 14
g > 42 33 24
h > 43 34
i > 44
As you can see, we have:
9 groups (= cols+rows-1 or keys+keys+1);
in each line, the y+x sum is the same, increasing from 0 to 8;
in each line, the y decreases, while the x increases.
each line ends when x reaches initial y
Based on these assumptions, we can write a single loop, using $sum and $startY as criteria to change line: when the decreasing $x has same value of $startY, we increment $sum and we set next $startY to the lowest value between $sum and the higher $array key, then we set next $x to the difference between $sum and $y:
$sum = $startY = $y = $x = 0;
while( $sum < 2*count($array)-1 )
{
echo $array[$y][$x];
if( $x == $startY )
{
$sum++;
$startY = $y = min( $sum, count($array)-1 );
$x = $sum - $y;
}
else
{
$y--;
$x++;
}
}
The result for above square is:
abbcccddddeeeeeffffggghhi
Looking at this eval.in demo you can see three different examples.
Just use for loops. It doesn't matter what language you are learning, you need to learn how to use for loops (or while loops or foreach loops or any good control structure).
You are going 00, then 10, 01, then 20, 11, 02, then 30, 21, 12, 03, etc... YOu can see that the first number decreases by 1 and the second number increases by 1. That goes until you hit n0...0n. That covers the first half of the square...
// Assume $n is the width/height of the square
for($m=0; $m<=$n; $m++)
{
for($a=$n; $a>=0; $a--)
{
for($b=0; $b<=$n; $b++)
{
//Do whatever you want with $array[$a][$n]...
}
}
}
Now, the second half of the square hits 41, 32, 23, 14 on the first stripe. It hits 42, 33, 24 on the second stripe. It hits 43, 34, and finally 44. You can see they both step up until they hit $n
for($m=1; $m<=$n; $m++)
{
for($b=$m; $b<=$n; $b++) // Put B on the outside because it is the limitation
{
for($a=4; $b<=$n; $a--)
{
//Do what you want with $a and $b
}
}
}
Now... can this be deciphered easily? Yes. You are just scrambling up the letters. No matter how you scramble it, it is deciphered easily. You need to substitute the letters with a replacement set that changes. Optimally, you want a completely new replacement set per letter you replace - which is difficult to use. So, most ciphers use a set of replacement sets, say 32 sets of replacement letters or symbols, that cycle through as randomly as possible.
Related
I have a two column layout with this disposition of elements:
A B
B A
A B
B A
…
A and B elements have content from different origins and also different styles. So, I'm trying to find an expression to generate this A B B A A B B A … sequence both in PHP and CSS nth-child.
This is what I'm doing to generate the layout:
/* $post_ids is an Array of ID's */
$count = count( $post_ids );
for ( $i = 0; $i < $count; $i++ ) {
$classes = ['teaser', 'lg-6', 'md-6', 'sm-12'];
if ( ( 2 * $i ) % 2 == 0 ) { // This is wrong. Always true!
$classes[] = 'a-element';
} else {
$classes[] = 'b-element';
}
insert_post( $post_ids[ $i ], $classes ); // This is a custom function
}
And this is my CSS:
.teaser:nth-child(2n) { // Also wrong
/* Styles for A items */
}
I know once I got the correct PHP sequence I could replace my CSS with:
.a-element {…}
.b-element {…}
But I'd like to know if this could be also done with nth-child...
I guess it can't be that hard, but I'm kinda stuck with this... Any hint or help will be much appreciated!
Edit: After #axiac's answer and some research, I've learned that nth-child only allows:
a number - any positive integer (1,2,3,20, etc.)
a keyword - even or odd
an expression - in the form of an+b (a, b being integers)
So, I guess what I want can't be done with CSS's nth-child. Thank you guys!
The PHP code you need is:
if ((int)(($i + 1) / 2) % 2 == 0 ) {
$classes[] = 'a-element';
} else {
$classes[] = 'b-element';
}
Update
At OP's request, this is how I produced the code above. The desired outcome is:
A B B A A B B A ..
after the initial A each symbol repeats twice. We use a for (;;) loop to iterate from 0 to some $n greater than zero;
the alternation is provided by the modulo operation (% 2);
grouping the values can be done by finding a common property for consecutive numbers. The integer result of division by the desired group size is such a property. If you need to output 5 As followed by 5 Bs then you just notice that any positive integer number can be written as 5 * k + r where r is one of 0, 1, 2, 3, 4 (this is how the division of integer numbers works). There are exactly 5 consecutive integer numbers that divided by 5 produce the same integer result (and different remainders);
given that the PHP division always produce real numbers, the conversion of the result to int (by type casting the result to (int)) is needed;
the + 1 offset is needed to "push" one A before the first 2 Bs. 0 and 1 produce the same result (0) when divided by 2 (they make the first group), 2 and 3 produce 1 and make the second group and so on.
Generalization
If you need to produce N different types of blocks (A, B, C, D a.s.o.), each of them appear M consecutive times (A A A B B B C C C D D D ..., M is 3 here) then the formula is:
(int)(($i + $k) / M) % N
The value produced by this formula is one of 0, 1 ... N - 1 and it tells what symbol to use (A, B a.s.o.). Without + $k this formula generates M instances of A followed by M instances of B, M instances of C and so on until the last symbol. It prints M * N symbols in total then it starts over with A.
The value of $k is one of 0 .. M * N - 1 and it allows the sequence to start from any point inside the sequence. It represents the number of symbols to skip from the start of the sequence.
If I understand correctly the problem is with:
(2 * $i) % 2 == 0
this will return true for all iterations (2 * $i) is always even
try:
$i % 2 == 0
instead
Not quite sure what to set this title as, or what to even search for. So I'll just ask the question and hope I don't get too many downvotes.
I'm trying to find the easiest way to find the highest possible number based on two fixed numbers.
For example:
The most I can multiply by is, say, 18 (first number). But not going over the resulted number, say 100 (second number).
2 x 18 = 36
5 x 18 = 90
But if the first number is a higher number, the second number would need to be less than 18, like so:
11 x 9 = 99
16 x 6 = 96
Here I would go with 11, because even though the second number is only 9, the outcome is the highest. The second number could be anything as long as it's 18 or lower. The first number can be anything, as long as the answer remains below 100. Get what I mean?
So my question is, how would write this in php without having to use switches, if/then statements, or a bunch of loops? Is there some math operator I don't know about that handles this sort of thing?
Thanks.
Edit:
The code that I use now is:
function doMath($cost, $max, $multiplier) {
do {
$temp = $cost * $multiplier;
if ($temp > $max) { --$multiplier; }
} while ($temp > $max);
return array($cost, $temp, $multiplier);
}
If we look at the 11 * 9 = 99 example,
$result = doMath(11, 100, 18);
Would return,
$cost = 11, $temp = 99, $multiplier = 9
Was hoping there was an easier way so that I wouldn't need to use a loop, being as how there are a lot of numbers I need to check.
If I understood you right, you are looking for the floor function, combining it with the min function.
Both a bigger number c and a smaller number a are part of the problem, and you want to find a number b in the range [0, m] such that a * b is maximal while staying smaller (strictly) than c.
In your example, 100/18 = 5.55555, so that means that 18*5 is smaller than 100, and 18*6 is bigger than 100.
Since floor gets you the integral part of a floating point number, $b = floor($c/$a) does what you want. When a divides c (that is, c/a is an integer already), you get a * b == c.
Now b may be outside of [0,m] so we want to take the smallest of b and m :
if b is bigger than m, we are limited by m,
and if m is bigger than b, we are limited by a * b <= c.
So in the end, your function should be :
function doMath($cost, $max, $multiplier)
{
$div = min($multiplier, floor($max/$cost));
return array($cost, $div * $cost, $div);
}
I am doing this programming challenge which can be found at www.interviewstreet.com (its the first challenge worth 30 points).
When I submitted the solution, I was returned a result which said that the answer was wrong because it only passed 1/11 test cases. However, I feel have tested various cases and do not understand what I am doing wrong. It would be helpful to know what those test cases could be so that I can test my program.
Here is the question (in between the grey lines below):
Quadrant Queries (30 points)
There are N points in the plane. The ith point has coordinates (xi, yi). Perform the following queries:
1) Reflect all points between point i and j both including along the X axis. This query is represented as "X i j"
2) Reflect all points between point i and j both including along the Y axis. This query is represented as "Y i j"
3) Count how many points between point i and j both including lie in each of the 4 quadrants. This query is represented as "C i j"
Input:
The first line contains N, the number of points. N lines follow.
The ith line contains xi and yi separated by a space.
The next line contains Q the number of queries. The next Q lines contain one query each, of one of the above forms.
All indices are 1 indexed.
Output:
Output one line for each query of the type "C i j". The corresponding line contains 4 integers; the number of points having indices in the range [i..j] in the 1st,2nd,3rd and 4th quadrants respectively.
Constraints:
1 <= N <= 100000
1 <= Q <= 100000
You may assume that no point lies on the X or the Y axis.
All (xi,yi) will fit in a 32-bit signed integer
In all queries, 1 <=i <=j <=N
Sample Input:
4
1 1
-1 1
-1 -1
1 -1
5
C 1 4
X 2 4
C 3 4
Y 1 2
C 1 3
Sample Output:
1 1 1 1
1 1 0 0
0 2 0 1
Explanation:
When a query says "X i j", it means that take all the points between indices i and j both including and reflect those points along the X axis. The i and j here have nothing to do with the co-ordinates of the points. They are the indices. i refers to point i and j refers to point j
'C 1 4' asks you to 'Consider the set of points having index in {1,2,3,4}. Amongst those points, how many of them lie in the 1st,2nd,3rd and 4th quads respectively?'
The answer to this is clearly 1 1 1 1.
Next we reflect the points between indices '2 4' along the X axis. So the new coordinates are :
1 1
-1 -1
-1 1
1 1
Now 'C 3 4' is 'Consider the set of points having index in {3,4}. Amongst those points, how many of them lie in the 1st,2nd,3rd and 4th quads respectively?' Point 3 lies in quadrant 2 and point 4 lies in quadrant 1.
So the answer is 1 1 0 0
I'm coding in PHP and the method for testing is with STDIN and STDOUT.
Any ideas on difficult test cases to test my code with? I don't understand why I am failing 10 / 11 test cases.
Also, here is my code if you're interested:
// The global variable that will be changed
$points = array();
/******** Functions ********/
// This function returns the number of points in each quadrant.
function C($beg, $end) {
// $quad_count is a local array and not global as this gets reset for every C operation
$quad_count = array("I" => 0, "II" => 0, "III" => 0, "IV" => 0);
for($i=$beg; $i<$end+1; $i++) {
$quad = checkquad($i);
$quad_count[$quad]++;
}
return $quad_count["I"]." ".$quad_count["II"]." ".$quad_count["III"]." ".$quad_count["IV"];
}
// Reflecting over the x-axis means taking the negative value of y for all given points
function X($beg, $end) {
global $points;
for($i=$beg; $i<$end+1; $i++) {
$points[$i]["y"] = -1*($points[$i]["y"]);
}
}
// Reflecting over the y-axis means taking the negative value of x for all given points
function Y($beg, $end) {
global $points;
for($i=$beg; $i<$end+1; $i++) {
$points[$i]["x"] = -1*($points[$i]["x"]);
}
}
// Determines which quadrant a given point is in
function checkquad($i) {
global $points;
$x = $points[$i]["x"];
$y = $points[$i]["y"];
if ($x > 0) {
if ($y > 0) {
return "I";
} else {
return "IV";
}
} else {
if ($y > 0) {
return "II";
} else {
return "III";
}
}
}
// First, retrieve the number of points that will be provided. Make sure to check constraints.
$no_points = intval(fgets(STDIN));
if ($no_points > 100000) {
fwrite(STDOUT, "The number of points cannot be greater than 100,000!\n");
exit;
}
// Remember the points are 1 indexed so begin key from 1. Store all provided points in array format.
for($i=1; $i<$no_points+1; $i++) {
global $points;
list($x, $y) = explode(" ",fgets(STDIN)); // Get the string returned from the command line and convert to an array
$points[$i]["x"] = intval($x);
$points[$i]["y"] = intval($y);
}
// Retrieve the number of operations that will be provied. Make sure to check constraints.
$no_operations = intval(fgets(STDIN));
if($no_operations > 100000) {
fwrite(STDOUT, "The number of operations cannot be greater than 100,000!\n");
exit;
}
// Retrieve the operations, determine the type and send to the appropriate functions. Make sure i <= j.
for($i=0; $i<$no_operations; $i++) {
$operation = explode(" ",fgets(STDIN));
$type = $operation[0];
if($operation[1] > $operation[2]) {
fwrite(STDOUT, "Point j must be further in the sequence than point i!\n");
exit;
}
switch ($type) {
case "C":
$output[$i] = C($operation[1], $operation[2]);
break;
case "X":
X($operation[1], $operation[2]);
break;
case "Y":
Y($operation[1], $operation[2]);
break;
default:
$output[$i] = "Sorry, but we do not recognize this operation. Please try again!";
}
}
// Print the output as a string
foreach($output as $line) {
fwrite(STDOUT, $line."\n");
}
UPDATE:
I finally found a test case for which my program fails. Now I am trying to determine why. This is a good lesson on testing with large numbers.
10
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
12
C 1 10
X 1 3
C 5 5
Y 2 10
C 10 10
C 1 10
X 1 3
C 5 5
Y 2 10
C 10 10
X 3 7
C 9 9
I am going to test this properly by initializing an error array and determining which operations are causing an issue.
I discovered a test case that failed and understood why. I am posting this answer here so it's clear to everyone.
I placed a constraint on the program so that j must be greater than i, otherwise an error should be returned. I noticed an error with the following test case:
10
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1
C 2 10
The error returned for the operation C. Essentially the program believed that "2" was greater than "10". The reason for this I discovered was the following:
When using fgets(), a string is returned. If you perform string operations such as explode() or substr() on that line, you are converting the numbers in that initial string into a string again. So this means that the 10 becomes "10" and then after string operations becomes "0".
One solution to this is to use the sscanf() function and basically tell the program to expect a number. Example: for "C 2 10" you could use:
$operation_string = fgets(STDIN);
list($type, $begpoint, $endpoint) = sscanf($operation_string, "%s %d %d");
I submitted the new solution using sscanf() and now have 3/11 test cases passed. It did not check any more test cases because the CPU time limit was exceeded. So, now I have to go back and optimize my algorithm.
Back to work! :)
To answer, "What are those test cases?" Try this "solution":
<?php
$postdata = http_build_query(
array(
'log' => file_get_contents('php://stdin')
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
file_get_contents('http://myserver/answer.php', false, $context);
?>
On your server:
<?php
$fp = fopen('/tmp/answers.log', 'a');
fputs($fp, $_POST['log']."\n");
fclose($fp);
?>
Edit:
I did that. And came up with this being your main problem (I think):
$operation = explode(" ",fgets(STDIN));
Change that to:
$operation = explode(" ",trim(fgets(STDIN)));
Because otherwise "9" > "41 " due to string comparison. You should make that fix in any place you read a line.
As far as I guess, this solution won't work. Even if you solve the Wrong Answer problem, the solution will time out.
I was able to figure out a way for returning the quadrants count in O(1) time.
But not able to make the reflections in lesser time. :(
1st number: 50
2. 30
3. 70
4. 40
5. 11
and other number is 33
I need to calculate which two numbers the last number is between (using php) .. any help?
Iterate over your list and find the following two values:
The largest number that is smaller than your target number.
The smallest number that is larger than your target number.
In pseudo-code:
lowerlimit = Unknown
upperlimit = Unknown
for each n in list:
if (n <= target) and (lowerlimit is Unknown or n > lowerlimit):
lowerlimit = n
if (n >= target) and (upperlimit is Unknown or n < upperlimit):
upperlimit = n
Then lowerlimit and upperlimit are your answer. This algorithm requires O(n) time and O(1) extra space.
If you are going to test the same list with many different target numbers then it could make sense to sort the list first requiring O(n log(n)) time, but then you can find the limits in just O(log(n)) time using a binary search.
I'll not give you the code but give you some guidelines for your homework.
You need to do these steps to solve your problem.
Sort your list of numbers. (I guess you are storing them in an array so sort the array.)
With a for loop search for the place where the element N is bigger than your number and the element N-1 is smaller. That will give you your position.
Oh and to avoid really long loop. use "break" after you find your position.
Sorted List:
11
30
// your 33 is bigger than 30 and smaller than 40, so this is the position you want.
40
50
70
function isBetween($several_numbers, $number)
{
$return_numbers = array();
sort($several_numbers);
$j = 0;
//find the first number in the array that $number is bigger than
while($number > $several_numbers[$j]) $j++;
if ($j == 0 || $j > count($several_numbers) - 1) return array();
$return_numbers[0] = $several_numbers[$j-1];
while($number > $several_numbers[$j]) $j++;
if ($j > count($several_numbers)-1) return array();
$return_numbers[1] = $several_numbers[$j];
return $return_numbers;
}
print_r(isBetween(array(50, 30, 70, 40, 10), 33));
I don't know if I understood correctly but this seems to be it
There is another recent Project Euler question but I think this is a bit more specific (I'm only really interested in PHP based solutions) so I'm asking anyway.
Question #5 tasks you with: "What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?"
Now, I have solved it twice. Once very inefficiently and once much more efficiently but I am still far away from an especially sophisticated answer (and I am not especially solid in math hence my brute force solution). I can see a couple of areas where I could improve this but I am wondering if any of you could demonstrate a more efficient solution to this problem.
*spoiler: Here is my less than optimal (7 seconds to run) but still tolerable solution (not sure what to do about the double $... just pretend you only see 1...
function euler5(){
$x = 20;
for ($y = 1; $y < 20; $y++) {
if (!($x%$y)) {
} else {
$x+=20;
$y = 1;
}
}echo $x;
};
Collect prime factors for all numbers between 1 and 20. Counting the maximal exponents of each prime factor, we have 16 = 2**4, 9 = 3**2, as well as 5, 7, 11, 13, 17, 19 (each appearing only once). Multiply the lot, and you have your answer.
in php it will look like this:
<?php
function gcd($a,$b) {
while($a>0 && $b>0) {
if($a>$b) $a=$a-$b; else $b=$b-$a;
}
if($a==0) return $b;
return $a;
}
function euler5($i=20) {
$euler=$x=1;
while($x++<$i) {
$euler*=$x/gcd($euler,$x);
}
return $euler;
}
?>
Its at least twice as fast than what you posted.
Chris Jester-Young is right.
In general if you wanted the smallest number that is evenly divisible by all of the numbers from 1 to N, you would want to find all the prime numbers from 2 to N, and for each one, find the greatest number of times it divides any number in the range. This can be calculated by finding the greatest power of the prime that's not greater than N.
In the case of 20, as Chris pointed out, 2^4 is the greatest power of 2 not greater than 20, and 3^2 is the greatest power of 3 not greater than 20, and for all other primes, only the first power is not greater than 20.
You can remove some numbers that are divided with, for example 1 is unnecessary, all natural numbers are divisible by 1.you don’t need 2 either, and therefore, all numbers are divisible by multiples of 2 (4, 8, 16, etc) are divisible by 2, also. So the relevant numbers will be 11, 12, 13, 14, 15, 16, 17, 18, and 19.
So:
<?
function eulerPuzzle()
{
$integers = array( 11,12,13,14,15,16,17,18,19 );
for ($n = 20; 1; $n += 20 ) {
foreach ($integers as $int) {
if ( $n % $int ) {
break;
}
if ( $int == 19 ) {
die ("Result:" . $n);
}
}
}
}
eulerPuzzle();
?>
<?php
$i=20;
while ($i+=20) {
for ($j=19;$j!==10;--$j){
if ($i%$j) continue 2;
}
die ("result: $i\n");
}
Is the fastest and shortest php solution so far. About 1.4x faster than Czimi's on my comp. But check out the python solution, thats a nice algo.
Some people really over-think this...
In Ruby:
puts 5*7*9*11*13*16*17*19
#People doing simple math; I'm not sure if that is the goal of the exercise. You are to learn new languages and new ways to perform stuff. Just doing it by a calculator isn't the right way going about things.
And I know this is a post in an old old thread but it still comes up in google results :)
Doing it in code (PHP that is) I found this to be the fastest solution:
function eulerPuzzle() {
$integers = array (11, 12, 13, 14, 15, 16, 17, 18, 19 );
for($n = 2520; 1; $n += 2520) {
foreach ( $integers as $int ) {
if ($n % $int) {
break;
}
if ($int == 19) {
die ( "Result:" . $n );
}
}
}
}
eulerPuzzle ();
Yes, it's a modified piece from CMS. The main reason it is faster is because when you read the question, they already state that the lowest possible number for the first 10 integers is 2520. therefor, you can just increment by 2520 instead of 20. resulting in 126 times less loops
I know you said PHP, but here's my rough draft in Python.
#!/usr/bin/env python
from operator import mul
def factor(n):
factors = {}
i = 2
while i < n and n != 1:
while n % i == 0:
try:
factors[i] += 1
except KeyError:
factors[i] = 1
n = n / i
i += 1
if n != 1:
factors[n] = 1
return factors
base = {}
for i in range(2, 2000):
for f, n in factor(i).items():
try:
base[f] = max(base[f], n)
except KeyError:
base[f] = n
print reduce(mul, [f**n for f, n in base.items()], 1)
It's not as elegant as I could have made it, but it calculates the least common multiple of the numbers from 2 to 2000 in .15s. If your iterative solution could process a billion candidates per second, it would take 10^849 years to finish.
In other words, don't bother optimizing the wrong algorithm.