[a / b / c / d / e / f / g / gif / h / hr / k / m / o / p / r / s / t / u / v / vg / vm / vmg / vr / vrpg / vst / w / wg] [i / ic] [r9k / s4s / vip / qa] [cm / hm / lgbt / y] [3 / aco / adv / an / bant / biz / cgl / ck / co / diy / fa / fit / gd / hc / his / int / jp / lit / mlp / mu / n / news / out / po / pol / pw / qst / sci / soc / sp / tg / toy / trv / tv / vp / vt / wsg / wsr / x / xs] [Settings] [Search] [Mobile] [Home]
Settings Mobile Home
/wsr/ - Worksafe Requests

[Advertise on 4chan]

Thread archived.
You cannot reply anymore.

[Advertise on 4chan]

File: Capture.png (97 KB, 770x676)
97 KB
Got some homework here. Unfortunately, I have very little experience and have no idea how to solve these. I am not allowed to use replace function/method, global variables and the only collection I'm allowed to use are strings (so no tuples, arrays, etc.). Homework is 10 questions so I will use multiple posts to show them clearly. Any help or explanation would be very appreciated as while I'm ashamed to ask for help this seems way too hard for someone who just started like me.
File: Capture.png (129 KB, 825x785)
129 KB
129 KB PNG
some of the questions may spread across multiple pics, I apologize for this
File: Capture.png (116 KB, 847x824)
116 KB
116 KB PNG
File: Capture.png (98 KB, 593x734)
98 KB
File: Capture.png (107 KB, 596x653)
107 KB
107 KB PNG
File: Capture.png (64 KB, 582x606)
64 KB
Final picture.
1.1 - it's easier to write a brute force loop that tests each odd number from 1 to n' where n' = |n|/2^a is odd (take the absolute value and divide by 2 until the result is odd).

If you can get a prime factor representation then it's easy to directly compute the sum.

1.2 - That's a for loop. Hint: Start adding from 1/n^2 up to 1/1^2 if n>=1 and then add 1000. The result will be more exact.

1.3: If n < 1 just return the result. If n>1, don't use a double recursive function, but a loop, since you can compute P2, then P3, then P4,... from the previous values.
P2 = 2P1 + P0 = 2x1 + 0 = 2
P3 = 2P2 + P1 = 2x2 + 1 = 5
and so on. Note that you only need three values at any given time and not an array (e.g. in P3 and later you don't need P0 anymore).
I don't understand, could you elaborate please? I'm not even sure where to start...
Start by forming a proper understanding of the question and pseudocode for the solution. And yes, use a for loop for the Pell number thing, recursion can be nasty if you don't need it.
1.1 When talking divisors, there's usually a use of the modulo operator, %. dividend%divisor=remainder. If the remainder is 0, the divisor should be added to the sum. If you advance your divisors in the loop by 2 each time, after starting at 1, you go over only the odd ones. When you're done with a loop searching for all the odd divisors, run a quick check of whether the sum is still equal to 0 or it got changed, then use it to determine what to return.
1.2 Start with the negative/non-negative check, not unlike the check from the previous one. Then have your sum variable ready with the value of 1000, and start a loop summing up each fraction. When you're done, return.
1.3 You could write a recursive function. Might be controversial depending on your teacher, since it gets grossly inefficient in larger numbers. The recursive function basically does this 3-way condition as seen in eq.1. If n>1, it calls to itself but with the decremented values. The decremented values will continue to automatically decrease until they reach either 0 or 1, which have definite values and thus end the recursion. It's really just a 3-way condition and returning the value.
If you don't feel like doing recursion, you could do a running calculation with a loop. Keep two variables for P(m-2) and P(m-1), use them to calculate P(m). If m is equal to n, return solution, otherwise move P(m-1) into P(m-2) and P(m) into P(m-1), and do it again for the incremented m, until you eventually reach n.
1.4 I'm not familiar with Python specifically, but general approach imo is to consider the string as an array. Cycle through the array's indexes (loop I guess), and see if the array's values match any of the specified special characters. There might be a way to play with the ASCII values of the characters, like saying "larger or equal to e, smaller or equal to j" but again I don't really know Python. If the character is extraordinary, increment your count. In the end, return count.
>it gets grossly inefficient in larger numbers
So long as it's a pure function, you can cache some/all of the results in an array or hashtable.

if isAlreadyComputed(n) return alreadyComputedResult(n)
<recursive step>
return (m)
Update: I have managed to complete 1.1-1.4, I still need help with 1.5-1.10
Continuing. Remember, my background is from C/C++, so I'm not familiar with Python tricks and manipulations.
1.5 idk, probably uses Python magic
1.6/1.7 Create a dictionary or a match-case to get the values to sum. Maybe for 1.6 there's some Python magic I'm not familiar with.
1.8 Loop for number of characters. Despite the description saying that the first letter of the word is the first letter of the encrypted word, the examples say you start with the end. Even indexes add character #length-i (by having a "-i" you go backwards as i increases), odd indexes add character #i.
1.9 I would use two running indexes to follow the original string and the new string, as the new string may or may not progress depending on the existence of non-letter characters. Run through the original string, copying the indexed character. Check if the next character exists, otherwise just end it there (should solve both end-of-string and 1-character-long strings). Then check the characters of index and of index+1, if both are letters (isalpha function for those two), check the first character's case (isupper function) and add an "o" respectively, then check the second character's case and add a "p" respectively. Advance indexes based on whether something was added or not, and rinse repeat.
1.10 Unless there's something I'm missing, you probably need to run through (with a big loop) the string length/2 (rounded down) times to check for sub-strings of as many characters in length. A string of 10 characters could have two repeating sub-strings of 5 characters each. So your loop chooses your sub-string length. New loop, searching through the string for duplicates It grabs the first substring, then checks if the next one in line (index+sublength) is the same. If not, increment index by 1. You can stop it around length-2*sublength, give or take 1, figure it out.
>cont with 1.10 example
For a string 0123456789, say we check substring of length 3. You grab 012 at index 0, then grab 345 at index 0+3. Not equal? Advance index to 1, then you grab 123 and compare it with index 1+3 which gives you 456. You continue until index 4, where you grab 456 and compare it with 789, see no match, and you stop this inner loop. Continue to sublength 4, compare 0123 to 4567, 1234 to 5678, 2345 to 6789 and you're done with that as well. Continue with sublength 5 comparing 01234 to 56789, and that's it. You can't compare anything longer.
If at ANY point you find an equality of subsequent substrings, return false. If you reach the end of it all without returning, it means you never found a repetition, so you return true.
Thanks a ton for the help, still kinda stuck on these last few so any python experts help would be greatly appreciated
>1.5 idk, probably uses Python magic
Wow you give up quickly.
- reverse string
- recognise ([0-9]{3}[,])*[0-9]{1,3}[-]*) | return false
- d = 1
- t = 0
- for c in string:
-- if c == "-" return (-1*t)
-- if c != [0-9] continue
-- t += (d*((int)c - 48)
-- d *= 10
- return t

You can do this in any language, and it's a good basic introduction to FSMs and to casting in C. You don't need a regex library, just write out the state machine on paper then implement it by hand, there's only 2-3 actual states.

The question isn't 100% unambiguous as to whether "1000000,000" is acceptable, but it's reasonable to assume it's not.
Dictionaries are unfortunately not allowed, the only collection allowed is strings
>Dictionaries are unfortunately not allowed, the only collection allowed is strings
You might want to clarify that requirement, because learning Python without dictionaries is like learning English without the letter e.

You could certainly write your own array-based dictionary or enum implementation real easy, but fitting it and the thing that calls it onto one line would be a doozy.

Dictionaries are absolutely the correct structure to use for this question, and any other approach is just being dumb for the sake of being dumb.

def symbol_to_number(s):
_if (s == "!"): return (121)
_if (s == "N"): return (6)
_raise RuntimeError ("undefined symbol")
Aside from anything else, if you were wanting to call one of the clever Functional library functions, you'd be passing it a dictionary, or a lambda function that acts exactly like a dictionary. Actually, that's your best bet for getting it on one line without using a dictionary: call reduce() on the string with a lambda function that reimplements a dictionary using if statements.
Update 2: I've completed 1.5-1.7. Still working on these last few so any help on them would be nice.
Help for them was already provided.

Delete Post: [File Only] Style:
[Disable Mobile View / Use Desktop Site]

[Enable Mobile View / Use Mobile Site]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.