Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,195,125 members, 7,957,222 topics. Date: Tuesday, 24 September 2024 at 09:23 AM |
Nairaland Forum / Science/Technology / Programming / Please Have You Seen This Algorithm Question (733 Views)
Thread For Nairaland Algorithm Questions / I Need A Programmer To Develop This Algorithm Into App / This Algorithm Can Create 3D Animations From A Single Still Image (2) (3) (4)
Please Have You Seen This Algorithm Question by cbrass(m): 4:06pm On Oct 02, 2021 |
I have tried everything I can but it only shows result for the first file |
Re: Please Have You Seen This Algorithm Question by stanliwise(m): 6:11pm On Oct 02, 2021 |
cbrass:Share your code. It’s straight forward |
Re: Please Have You Seen This Algorithm Question by Bigshoe2028: 6:35pm On Oct 02, 2021 |
While loop is ur friend here to save memory |
Re: Please Have You Seen This Algorithm Question by fnep2smooth(m): 10:49pm On Oct 02, 2021 |
Are you looking for job in Google or Facebook. If na naija company expect to be pay 30k with a intern tag. |
Re: Please Have You Seen This Algorithm Question by cbrass(m): 11:40pm On Oct 02, 2021 |
stanliwise:
This code loops through all the files and sums them for just file A. instead of breaking it by file just as the example given |
Re: Please Have You Seen This Algorithm Question by cbrass(m): 11:41pm On Oct 02, 2021 |
fnep2smooth: this made my day after serious coding to solve the problem |
Re: Please Have You Seen This Algorithm Question by stanliwise(m): 8:11am On Oct 03, 2021 |
cbrass:I am not clear of your code and looking at it I don't see how it would solve your problems. The only thing to consider now is that your questions should not have what we call circular-reference. So no file now must link to another file that link back to it self. you need a code to do that checking. that been said a simple pseudocode should do. Please flip your phone horizontally to see the codes properly.
|
Re: Please Have You Seen This Algorithm Question by Iambro(m): 4:18pm On Oct 03, 2021 |
Create a recursive function that read the files or check if it is a directory then reads its files etc. |
Re: Please Have You Seen This Algorithm Question by cbrass(m): 8:16pm On Oct 03, 2021 |
Iambro: I have done that, it didn't give the desired answer..one thing is this question looks so easy but when you start to code then you will get hooked somewhere |
Re: Please Have You Seen This Algorithm Question by cbrass(m): 8:20pm On Oct 03, 2021 |
stanliwise: I did something similar to this before, when i get home i will try yours. Thanks a lot i really appreciate it |
Re: Please Have You Seen This Algorithm Question by cbrass(m): 9:06pm On Oct 03, 2021 |
stanliwise: |
Re: Please Have You Seen This Algorithm Question by stanliwise(m): 10:18pm On Oct 03, 2021 |
[quote author=cbrass post=106417200][/quote] You’re not checking for circular references and I didn’t see where you read the files line by line, you were returning simple recursion without reading the lines |
Re: Please Have You Seen This Algorithm Question by SapphireYj: 1:30pm On Oct 04, 2021 |
Implemented in nodejs. Tested and its working fine as long as A.txt, B.txt, and C.txt are in same directory with this file. If you know javascript, its just converting this code to php. const fs = require('fs') const printRecursively = (path)=>{ let result = 0 try { const data = fs.readFileSync(path) let newfilePath let dt = data.toString().split('\r\n') dt.forEach(str=>{ let dtNum = +str if(!isNaN(dtNum)){ result+=dtNum } else{ newfilePath = str } }) if(newfilePath){ result = result + printRecursively(newfilePath) console.log(`${path} - ${result}`) } else{ console.log(`${path} - ${result}`) } return result } catch (err) { return console.log(err.message) } } printRecursively('A.txt') |
Re: Please Have You Seen This Algorithm Question by cbrass(m): 3:27pm On Oct 04, 2021 |
[quote author=SapphireYj post=106435278]Implemented in nodejs. Tested and its working fine as long as A.txt, B.txt, and C.txt are in same directory with this file. If you know javascript, its just converting this code to php. const fs = require('fs') const printRecursively = (path)=>{ let result = 0 try { const data = fs.readFileSync(path) let newfilePath let dt = data.toString().split('\r\n') dt.forEach(str=>{ let dtNum = +str if(!isNaN(dtNum)){ result+=dtNum } else{ newfilePath = str } }) if(newfilePath){ result = result + printRecursively(newfilePath) console.log(`${path} - ${result}`) } else{ console.log(`${path} - ${result}`) } return result } catch (err) { return console.log(err.message) } } printRecursively('A.txt')[/quote I think i have seen something similar before. my javascript skill is not strong. so converting to PHP is really hard |
Re: Please Have You Seen This Algorithm Question by cbrass(m): 3:31pm On Oct 04, 2021 |
SapphireYj: I will compare your solution with mine ,i can sum the files independently though . i sent you a DM SapphireYj |
(1) (Reply)
New Forum Is Out Now, Take A Review On It / Open Source Business Software - CRM, ERP / Problems With Threads And Arrays
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 57 |