can you use compound conditions in an if statement in pseudocode? - pseudocode

for example this simple program which tests if 3 numbers are in ascending order
if a1<a2 then
if a2<a3 then
write "the numbers are in ascending order";
else
write "the numbers are not in ascending order";
endif
else
write "the numbers are not in ascending order;
endif
am i allowed to write it like this in pseudocode?
if (a1<a2 and a2<a3)
write "the numbers are in ascending order";
else
write "the numbers are not in ascending order";

Yes. As long as it's understandable and can easily be converted to real source code, it's fine as pseudocode.

Related

Having trouble with a coding problem, can someone help me out?

I am very new here and to coding in general so apologies in advance for any mistakes in my questions and code.
I am currently working on this problem:
Primary U.S. interstate highways are numbered 1-99. Odd numbers (like the 5 or 95) go north/south, and evens (like the 10 or 90) go east/west. Auxiliary highways are numbered 100-999, and service the primary highway indicated by the rightmost two digits. Thus, I-405 services I-5, and I-290 services I-90. Note: 200 is not a valid auxiliary highway because 00 is not a valid primary highway number. Given a highway number, indicate whether it is a primary or auxiliary highway. If auxiliary, indicate what primary highway it serves. Also indicate if the (primary) highway runs north/south or east/west.
EX: if the input is:
290
the output is:
I-290 is auxiliary, serving I-90, going east/west.
My code is currently as shown:
#include iostream
using namespace std;
int main()
{
int A; // A is the value for the Auxiliary highway
// This message will display when the code is run//
cout << "Please enter the three digit Auxiliary highway number" << endl;
cin >> A; // User inputs the Auxiliary highway number
do A-= 100;
while (A>100);
I am not sure what I am doing but I have no clue how to go about this so I am first starting with the second part(trying to make the code understand that if I am at the auxiliary number of I-290 then I would be servicing I-90). I tried this by trying to subtract 100 when the value for A was over 100 which would in theory leave me with a 2-digit number that would be the interstate highway number. I know that an error will happen when a number that ends in two zeros is entered so I planned to just use an if-else statement at the beginning of the code that would essentially just prevent this but, again, no clue how to do it but I think it may work
The first thing you want to do with a problem like this is think through it step by step. How do you solve this problem as a human with a pen and paper if you are given a highway number?
First you need a piece of code to tell you if the number is primary, so the code needs to tell you if the number is less than or equal to 99, if yes, then it is primary, if no, then it is auxillary.
In the case that is is not primary, you need a piece of code to tell you what the last 2 digits are of the number. The easiest way to do this is to convert the number to a string and remove the first character, and then convert back into an integer.
Lastly, you need a piece of code that tells you whether the primary road runs north/south, or east/west. So you need to check whether the number is even or odd. The easiest way to do this is to use the modulo function (x%2==y). If y is equal to zero, then you know the road runs east/west, otherwise it runs north south.
Hopefully you can see that the problem is a series of little problems that you can solve 1 by 1 to get the full solution. Problems become a lot less scary then.
I'm afraid I don't know C++ that well to give you a coded solution, but hopefully you can figure it out from here. It will be good practice for you to work through it because a lot of programming is about banging your head against a wall until you figure out the solution.
There are many ways to solve this problem. Here is one:
first, fix your header
#include <iostream>
accept user "highwayNumber"
cin >> highwayNumber;
write an if loop to determine highway properties:
if(highwayNumber > 0 && highwayNumber < 100){
highwayType = "primary";
//determine if highwayNumber is even or odd
if(highwayNumber % 2 == 0){
primaryType = “east-west”;
} else {
primaryType = “north-south”;
}
} else if (highwayNumber >= 100 && highwayNumber <= 999){
highwayType = “auxiliary”;
//determine what primaryHighway the auxiliaryHighway services
auxiliaryServiced = highwayNumber % 100;
} else {
cout << "invalid highway number” << end;
}
print output to user

how to crack RandomUtils.nextInt(a,b) by knowing some num seq

for example,
These three values (79,81,98) come from org.apache.commons.lang3.RandomUtils.nextInt(2,101) ,
how can I get the seed and predict the following numbers ?

Sort numbers with letters numerically and alphabetically

I have a Database on my website with a Long List of #'s (Product #'s) all containing letters (Exp. TC-345, TC-234 or HC-236W 123-234-PWD...)
Can we numerically and alphabetically sort the #'s on the website?
Currently we store it alphabetically, so the order is (10-PDW, 100-PDW, 110-PDW 2-PDW)
We would like to change it into (2-PDW, 10-PDW, 100-PDW, 110-PDW)
My developers say "The colorway number can never be sorted numerically. We would need to add another numeric field to the database for all the colorways and then sort that field numerically. Right now those numbers are in alphabetical order."
How do you sort a numbers with letters? We'd like to avoid adding a Numeric Field - that's just extra work. Is there any new technique out to do this?
It is possible. One way would be using a function for weighting strings, which gives far more weight for numbers than letters. Something like this:
letbers = ["10-PDW", "100-PDW", "110-PDW", "2-PDW"]
def weight(letber):
if letber == "":
return 0
n = ord(letber[-1])
if letber[-1] in "0123456789":
n *= 256^6 # 6 because maximum key length is 6
return 256*n + weight(letber[:-1])
print sorted(letbers, key = weight)
If you add the following transformation function (in scala), then you can alphabetically and numerically sort all strings:
def transformed(s: String): String = {
s.replaceAll("""(?<=[^\d]|^)(\d)(?=[^\d]|$)""","""000$1""")
.replaceAll("""(?<=[^\d]|^)(\d\d)(?=[^\d]|$)""","""00$1""")
.replaceAll("""(?<=[^\d]|^)(\d\d\d)(?=[^\d]|$)""","""0$1""")
}
Basically it replaces every number occurence by a fixed width integer, so that the alphabetical sorting equals the numerical sorting in that case.
Test on your input:
> val s = List("10-PDW", "100-PDW", "110-PDW", "2-PDW")
> s.sortBy(transformed)
res2: List[String] = List(2-PDW, 10-PDW, 100-PDW, 110-PDW)
This works only if you are sure that all numbers are below 9999. If you have more digits, then you should consider to expand the function or do something else.

How to populate an array with incrementally increasing values Ruby

I'm attempting to solve http://projecteuler.net/problem=1.
I want to create a method which takes in an integer and then creates an array of all the integers preceding it and the integer itself as values within the array.
Below is what I have so far. Code doesn't work.
def make_array(num)
numbers = Array.new num
count = 1
numbers.each do |number|
numbers << number = count
count = count + 1
end
return numbers
end
make_array(10)
(1..num).to_a is all you need to do in Ruby.
1..num will create a Range object with start at 1 and end at whatever value num is. Range objects have to_a method to blow them up into real Arrays by enumerating each element within the range.
For most purposes, you won't actually need the Array - Range will work fine. That includes iteration (which is what I assume you want, given the problem you're working on).
That said, knowing how to create such an Array "by hand" is valuable learning experience, so you might want to keep working on it a bit. Hint: you want to start with an empty array ([]) instead with Array.new num, then iterate something num.times, and add numbers into the Array. If you already start with an Array of size num, and then push num elements into it, you'll end up with twice num elements. If, as is your case, you're adding elements while you're iterating the array, the loop never exits, because for each element you process, you add another one. It's like chasing a metal ball with the repulsing side of a magnet.
To answer the Euler Question:
(1 ... 1000).to_a.select{|x| x%3==0 || x%5==0}.reduce(:+) # => 233168
Sometimes a one-liner is more readable than more detailed code i think.
Assuming you are learning Ruby by examples on ProjectEuler, i'll explain what the line does:
(1 ... 1000).to_a
will create an array with the numbers one to 999. Euler-Question wants numbers below 1000. Using three dots in a Range will create it without the boundary-value itself.
.select{|x| x%3==0 || x%5==0}
chooses only elements which are divideable by 3 or 5, and therefore multiples of 3 or 5. The other values are discarded. The result of this operation is a new Array with only multiples of 3 or 5.
.reduce(:+)
Finally this operation will sum up all the numbers in the array (or reduce it to) a single number: The sum you need for the solution.
What i want to illustrate: many methods you would write by hand everyday are already integrated in ruby, since it is a language from programmers for programmers. be pragmatic ;)

how to sort a treemap using bubble sort?

27527-683
27525-1179
27525-1571
27525-1813
27525-4911
27526-1303
27526-3641
27525-3989
27525-4083
27525-4670
27526-4102
27526-558
27527-2411
27527-4342
this is the list of key where it is declared as string in a map
then i want to sort it in ascending order.
how can i use a bubble sorting method inside a map?
where the value of the key is a list.
in order to get :
27525-1179
27525-1571
27525-1813
27525-3989
27525-4083
27525-4670
27525-4911
27526-558
27526-1303
27526-3641
27526-4102
27527-683
27527-2411
27527-4342
You should be able to just perform an in-order traversal on your tree. Bu if you insist here is what you would do.
keyList = yourTreeMap.getKeys();
for(i = keyList.length-1; i > 0; i--)
for(j = 0; j < i; j++)
if (keyList[j] > keyList[j+1]) keyList.swap(j, j+1);
Since you don't specify a lnaguage, I present psuedocode.
In general you just use the same bubble sort algorithm as normal it's just your comparison condition that's tweaked here to look at both the key and the value to determine what is greater than what, that is compare the keys first and if they're equal then compare the values if the keys don't match then use the difference in the values to get your result of swap or don't swap. Bubble sort is bad efficiency-wise though if you're using this in a real world scenario.
Jon got the post in before me but basically what he wrote looks right except you'd want a complex condition for the if within the nested loop, like
if(key1<key2)
keyList.swap(i,j)
else if(keyList[key1]<keyList[key2])
keyList.swap(i,j)
of course as he also stated how these keys/values are actually extracted/used will depend on the language, which is lacking in the question or tags.

Resources