Hello, I need your support, i have home work and seems my below is not correct ,
ID: 3707493 • Letter: H
Question
Hello,
I need your support, i have home work and seems my below is not correct , please can you check it ;
Option 1: Numbers. Your algorithm should:
Search a string of at least five numbers (for example, 37540)
Identify all of the substrings that form numbers that are divisible by 3.
For example, applying the algorithm on the string 37540 should produce the following substrings (not necessarily in this order): 0; 3; 75; 54; 375; 540.
Solution:
To accomplish the task, modulus is typically calculated using the following formula
Example number: 37540
Example divider: 3
Read String (input value)
For example, String = "37540" input string
Initial a to 1
If LEN(string) >= 5 then
The length of the string must be equal or more than 5
While a <= LEN(string) do
Loop from 1 to the length of the string
Set j to LEN(string) - a + 1
While b >=1 do
Loop from the length from the string point to the end (-1)
If MID (string,a,b) MOD 3 =0 then
The substring value dividable by 3 (using MOD function)
Print MID (string,a,b)
If MOD =0 then print the substring value
Set b to b -1
end while
Increase a
end while
End If
Stop
Explanation / Answer
Define function permute() which will return all possible permutations of a number. Use this function in main program.
permutedNumbers = []
void permute(string, start,end):
if start equals end
add string[start] to permutedNumbers
else
for i in start to end:
swap string[start] and string[i]
permute(string, l+1, r)
swap string[start] and string[i]
end permute
Read String (input value)
For example, String = "37540" input string
If LEN(string) >= 5 then
The length of the string must be equal or more than 5
call permute(string, 0 , LEN(string))
for i in 0 to LEN(permutedNumbers):
if permutedNumbers[i]%3 == 0:
print permutedNumbers[i]
Stop
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.