C
C#2w ago
Buhalter

String manipulation etc

https://paste.mod.gg/qiqfbdvjrwru/0 I have this task: Print to the file ManoKnyga.txt a merged text, composed according to the following rules: Text from the first file is copied until the first non-copied word from the second file is encountered, or the end of the file is reached; Text from the second file is copied until the first non-copied word from the first file is encountered, or the end of the file is reached; This is repeated until the end of both files is reached. The issue I'm facing with the code I have is that the method does not output the correct results. It has to check each word from each file simultaneously line by line to see check wether the word in one file matches another, if it does then the code has to be appended from the 2nd file and vice versa, until the end of both files is reached.
BlazeBin - qiqfbdvjrwru
A tool for sharing your source code with the world!
105 Replies
Buhalter
BuhalterOP2w ago
Here are the data and output files:
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Buhalter
BuhalterOP2w ago
school why?
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Buhalter
BuhalterOP2w ago
in a few days thursday to be axact
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Buhalter
BuhalterOP2w ago
I did try that but still could't find the issue in the logic I'm using VS btw
JP
JP2w ago
these nested conditions are suspicious to me
No description
JP
JP2w ago
though I could be misreading. If you had to describe your plan / algorithm in words, what would it be?
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Buhalter
BuhalterOP2w ago
The thing I'm trying to do I guess is check word by word from each file, and copy everything from the first file until the word in the first file matches with the word in the second file then it should start copying data from the second file onwards until it matches with the word from the first file if that's understandable
JP
JP2w ago
a little but I don't fully understand which word "the word in the second file" refers to -- any word in the second file not yet copied? any word on the next not-yet-copied line?
Buhalter
BuhalterOP2w ago
Idek how to better explain it like if there is word that is not yet coppied in the 2nd file and it equals to a word that's in the first file anywhere in the text if it is not located in the first line and vice versa
leowest
leowest2w ago
you said here is data and output your output does not match what you're describing its literally copying file A then file B to the output
Buhalter
BuhalterOP2w ago
well yeah it does that incorrectly it should not
leowest
leowest2w ago
ok then can you provide a correct output
Buhalter
BuhalterOP2w ago
I wanna fix exactly that
leowest
leowest2w ago
that would help us understand it
Buhalter
BuhalterOP2w ago
with this code exactly I literally get no output
leowest
leowest2w ago
manually writing the output
Buhalter
BuhalterOP2w ago
it just keeps loading and loading nothing happens in a sec
Angius
Angius2w ago
Basically, take the green parts, if I'm not mistaken
No description
Buhalter
BuhalterOP2w ago
Saulė patekėjo anksti, o dangus buvo be debesų. Paukščiai čiulbėjo garsiai; oras buvo šiltas ir gaivus. Ant kalno stovėjo sena pilis, kurios langai spindėjo ryto šviesoje. Žmonės miestelyje ruošėsi šventei – visi buvo gerai nusiteikę. Biblioteka atsidarė anksti, ir studentai skubėjo į paskaitas. Profesorius pasakojo apie matematiką ir istoriją. Keliautojas aplankė muziejų, kur eksponatai buvo įdomūs. Architektas projektavo naują pastatą miesto centre. Ryte dangus buvo mėlynas, o saulė švietė virš kalno. Pilis atrodė išdidžiai ir tyliai stebėjo miestelį. Vėjas pūtė švelniai; medžių lapai šnarėjo tarsi jūra. Visi laukė šventės pradžios – oras buvo kupinas džiaugsmo. Biblioteka buvo pilna studentų, kurie mokėsi matematiką. Profesorius dėstė apie architektūrą ir istoriją mieste. Keliautojas fotografavo eksponatai muziejuje buvo įspūdingi. Architektas pristatė projektą komisijai vakar vakare. like that basically
leowest
leowest2w ago
so that would be the output?
Buhalter
BuhalterOP2w ago
this looks about right too that's the output I guess I pasted it in
leowest
leowest2w ago
so you are checking file b for the first letter of file a u found it you start writing to output so you wrote file a to end
Buhalter
BuhalterOP2w ago
Text from the first file is copied until the first non-copied word from the second file is encountered, or the end of the file is reached; the task says this I do not even know what does the first non copied word mean but I believe I should copy data from the first file into the the new stringbuilder until I find the matching word then everything switches copying starts from the 2nd file and vice versa until the end is reached
leowest
leowest2w ago
yeah this is hard to understand what is it written in your own language? first non-copied word
Buhalter
BuhalterOP2w ago
I believe I was trying to do that
leowest
leowest2w ago
because I understand that as copy file A until u find a word that matches
Buhalter
BuhalterOP2w ago
yes that's exactly it then everything switches
leowest
leowest2w ago
then u go to file b until the same happens
Buhalter
BuhalterOP2w ago
yes that's right
leowest
leowest2w ago
if so your output is wrong again
Buhalter
BuhalterOP2w ago
yeah I just saw that
leowest
leowest2w ago
Saulė exists in file b so I should not copy it and move to file b Ryte does not exist in file a so that would be the first word in your output
Buhalter
BuhalterOP2w ago
I believe the first switch should happen when it reaches "o" the first letter o in the file b like this is coppied "Saulė patekėjo anksti," then it encounters letter o and there is one exactly like that almost at the start of file b I believe from that point on it should copy from the file b
leowest
leowest2w ago
that doesnt make sense so you're saying you dont look up file b for the word saule. o is not aligned with o in file b so if u're going word by word they are not in the same index to match "o" would fall under "melynas" maybe @ZZZZZZZZZZZZZZZZZZZZZZZZZ understood it better and could explain @Buhalter if you can post the requirements in your language the way your teacher asked might help us translate it.
Buhalter
BuhalterOP2w ago
In a sec Spausdinkite faile ManoKnyga.txt apjungtą tekstą, sudarytą pagal tokias taisykles: • kopijuojamas pirmojo failo tekstas tol, kol sutinkamas pirmasis nenukopijuotas antrojo failo žodis arba pasiekiama failo pabaiga; • kopijuojamas antrojo failo tekstas tol, kol sutinkamas pirmasis nenukopijuotas pirmojo failo žodis arba pasiekiama failo pabaiga; • kartojama tol, kol pasiekiama abiejų failų pabaiga. this is in my language
JP
JP2w ago
claude interprets this as "any not-yet-copied word from the other file", not as "the next-up word from the other file" fwiw https://claude.ai/share/39056638-f43d-4017-9b28-001d59027d75
Lithuanian text file merging assignment
Shared via Claude, an AI assistant from Anthropic
JP
JP2w ago
though this "the first" business is evil in the literal translation imo lmao
Buhalter
BuhalterOP2w ago
Lol
JP
JP2w ago
I'm going to see if I can solve this, then I'll see if I can figure what's wrong with yours. I actually don't see the bug right now, and I've been staring at this for a bit
Buhalter
BuhalterOP2w ago
I appreciate that a lot I hope it's gonna take a chunk out of your time
JP
JP2w ago
is a fun brainteaser now, don't worry :P oh! maybe this
// advance a line from file 1 if it is available, if it is not available, advance file 2
while ((line = reader.ReadLine()) != null && (line2 = reader2.ReadLine()) != null)
// advance a line from file 1 if it is available, if it is not available, advance file 2
while ((line = reader.ReadLine()) != null && (line2 = reader2.ReadLine()) != null)
as far as I can tell, this'll never advance file 2 if we get a line back for file 1 because && will short-circuit (that means, when the left hand side evaluates to true, the right hand side doesn't get evaluated, because its result won't affect the result of the AND expression) I can't read I interpreted that as OR
Buhalter
BuhalterOP2w ago
Oh I see
JP
JP2w ago
ignore me ignore me. Everything I said was right, only if you replace "and" with "or" lmfao
Buhalter
BuhalterOP2w ago
ok ok I believe the issue is with the logic later on I just couldn't fathom where for me yeah it seems ok but to someone more experienced it will definetly be visible
JP
JP2w ago
oh, for starters, should this be Append-ing from line? I think that should read from words, no? since i and j are words/words2 indexes
leowest
leowest2w ago
I would like to think u want to ReadAllText and then do some removal of punctuation -,;.! and then make everything lower case and split on space, newline to get all words otherwise u would need to add further checks I was considering the output something like this
ryte patekėjo anksti mėlynas be debesų paukščiai čiulbėjo garsiai švietė virš šiltas atrodė išdidžiai gaivus ant tyliai stebėjo miestelį vėjas pūtė švelniai medžių lapai šnarėjo tarsi jūra stovėjo sena laukė šventės pradžios kurios langai spindėjo ryto šviesoje žmonės miestelyje ruošėsi šventei kupinas džiaugsmo gerai nusiteikę atsidarė anksti pilna studentų kurie mokėsi studentai skubėjo į paskaitas pasakojo dėstė architektūrą mieste fotografavo aplankė muziejų kur muziejuje įspūdingi įdomūs pristatė projektą komisijai vakar vakare projektavo naują pastatą miesto centre
ryte patekėjo anksti mėlynas be debesų paukščiai čiulbėjo garsiai švietė virš šiltas atrodė išdidžiai gaivus ant tyliai stebėjo miestelį vėjas pūtė švelniai medžių lapai šnarėjo tarsi jūra stovėjo sena laukė šventės pradžios kurios langai spindėjo ryto šviesoje žmonės miestelyje ruošėsi šventei kupinas džiaugsmo gerai nusiteikę atsidarė anksti pilna studentų kurie mokėsi studentai skubėjo į paskaitas pasakojo dėstė architektūrą mieste fotografavo aplankė muziejų kur muziejuje įspūdingi įdomūs pristatė projektą komisijai vakar vakare projektavo naują pastatą miesto centre
But honestly the translation is not being very helpful.
Buhalter
BuhalterOP2w ago
but it would miss out on the punctuation and things no?
JP
JP2w ago
Oh, if you need that, then you need a different strategy, yes
Buhalter
BuhalterOP2w ago
I also wanna mention that I must read line by line to get the maximum points for this task
JP
JP2w ago
with your current approach (if not doing what leo said), you'd need to track the file character position separately from the word position
Buhalter
BuhalterOP2w ago
well that sounds complicated
JP
JP2w ago
so in addition to i, j for word position, you'd track a x, y for character position
leowest
leowest2w ago
if u dont @Buhalter then "matematiką." and "matematiką" wont match
JP
JP2w ago
hrm... a non-buffered line by line interpretation of this problem is impossible if the instructions truly meant "any non-copied word" and not "the next non-copied" word...
Buhalter
BuhalterOP2w ago
what if we overlooked the punctuation and just coppied the words? would it be possible then?
JP
JP2w ago
still impossible for what I'm referring to
leowest
leowest2w ago
the way its asking u, if its right in the translation, it forces u to loop it multiple times so u can do it as u read lines
JP
JP2w ago
Like, if the instrutions do want us to switch based on any word not yet copied from future lines, we need to be aware of those lines already
leowest
leowest2w ago
u would have to read the lines into a list
Buhalter
BuhalterOP2w ago
I guess that could work by reading it into a list
JP
JP2w ago
which might mean the translation was inaccurate
Buhalter
BuhalterOP2w ago
cuz technically we still are reading line by line
leowest
leowest2w ago
yeah u would sanitize and read it into a list of words from the file however u want if this is a simple duple for then what u want would work out of the box but the way its being ask doesn't align
Buhalter
BuhalterOP2w ago
the thing with the translation this is the correct one even when I read it in my own language
JP
JP2w ago
ah, hm. It could be a confused professor?
leowest
leowest2w ago
if we had a proper output to compare to
JP
JP2w ago
idk lol
Buhalter
BuhalterOP2w ago
it can lmao
leowest
leowest2w ago
then we could reverse it to understand it
Buhalter
BuhalterOP2w ago
I will inquire about this task on monday but right now this is all I got tbh
leowest
leowest2w ago
I would imagine 3 loops one outer loop controlling which words lists you're checking and 2 inner loops running check what has been copied from the 2nd file vs not. so 3 loops total some hashes storing words from each list of what has been copied or not and then u check the copied words and the list of words before advancing and as need u break out of the loop/swap file being read that is assuming my output is right which I dont know honestly 👆
Buhalter
BuhalterOP2w ago
Like at the moment we can only guess
leowest
leowest2w ago
some times teachers will write requirements in a more cryptic manner to get u to thinker about it and literal translation wont be able to pick that up and the translation is lost
Buhalter
BuhalterOP2w ago
when he explained he used just numbers for instance file a: 1 2 3 4 5 6 file b: 4 7 8 9 and the output would be : 1 2 3 4(maybe idk) 7 8 9 And I had to get it from there which doesn't make sense now what do to with the four My best guess is that we have to track somehow which are coppied in a hash or a list because we don't quite use hashes I don't even know how to use them and the concept of them
Angius
Angius2w ago
Isn't it just as simple as a loop, a boolean keeping track of whether we're copying from file 1 or 2, and... that's it? Split file text on spaces to get words Loop If strOne[i] is the same as strTwo[i] flip the bool If bool is true copy from strOne, if false copy from strTwo
Buhalter
BuhalterOP2w ago
I was trying to do that
JP
JP2w ago
at minimum, 2 different indexes there, if I'm understanding correctly
Angius
Angius2w ago
Why? You're iterating through both at the same time
JP
JP2w ago
why do you assume that the word position is synced? and based on the "current" word you'll pick up from the last file pointer position?
Angius
Angius2w ago
Ah True
leowest
leowest2w ago
1 2 3 4 5 6
Saulė patekėjo anksti, o dangus buvo
1 2 3 4 5 6 7 8 9
Ryte dangus buvo mėlynas, o saulė švietė virš kalno. Pilis
1 2 3 4 5 6
Saulė patekėjo anksti, o dangus buvo
1 2 3 4 5 6 7 8 9
Ryte dangus buvo mėlynas, o saulė švietė virš kalno. Pilis
I mean at the very least I dont see yet how the numbers are in play here I understand it jumping 5 and 6 and taking 7 8 9 but I dont understand how it picks up 4 and leaves 1 2 3 o is not in sync for it to swap there
Buhalter
BuhalterOP2w ago
but does it have to be? I don't get it Oh I see how that works
leowest
leowest2w ago
otherwise I dont see how what u said make sense here file b: 4 7 8 9
Angius
Angius2w ago
Numbers are stand-ins for words
leowest
leowest2w ago
either u misunderstood something or
Buhalter
BuhalterOP2w ago
I could've I definetly could have
leowest
leowest2w ago
if u see the table I wrote above with numbers above the words the first set is file a second set is file b
Angius
Angius2w ago
file a: cat dog parrot bread chair cucumber file b: bread table slug window Result: cat dog parrot bread table slug window
Buhalter
BuhalterOP2w ago
I was thinking like that but I could not be sure It copies until it sees the matching word and then copie sfrom file b
leowest
leowest2w ago
yes that would make sense and then swaps
Buhalter
BuhalterOP2w ago
like in this one no?
leowest
leowest2w ago
that's why I said this
MODiX
MODiX2w ago
leowest
some times teachers will write requirements in a more cryptic manner to get u to thinker about it
Quoted by
<@1102729783969861782> from #String manipulation etc (click here)
React with ❌ to remove this embed.
leowest
leowest2w ago
and translation there is lost
JP
JP2w ago
this mostly matches what I suspect, I think we need to switch back at EOF though
one two three four five
six seven eight nine ten
one two three four five
six seven eight nine ten
three apple orange cherry eight orchid sunflower
three apple orange cherry eight orchid sunflower
one two three apple orange cherry eight six seven eight nine ten orchid sunflower
* * *
match match end of file 1
one two three apple orange cherry eight six seven eight nine ten orchid sunflower
* * *
match match end of file 1
(whether the matched word is supposed to be duplicated or not, I haven't the faintest clue) (this ^ also does not make sense with a "process this line-by-line" instruction)
Angius
Angius2w ago
The matched word is duplicated, so whether we copy it from str1 or str2 doesn't matter ¯\_(ツ)_/¯
JP
JP2w ago
oh, I suppose so I just wasn't sure if they wanted both pointers advanced for the matched word
leowest
leowest2w ago
you would need just something to tell when to stop matching and complete with a or b because either run out of words
JP
JP2w ago
usually wouldn't code-drop on an assignment, but I despise ambiguous instructions -- here's what I've been using to debug your code, if it helps -- I at least stopped the infinite loops. It still switches on "next word" not "any upcoming word" though
JP
JP2w ago
I also switched to StringReader because I wanted the example text in the same file, but no reason for you to do that

Did you find this page helpful?