Please Have You Seen This Algorithm Question - Programming - Nairaland
Nairaland Forum › Science/Technology › Programming › Please Have You Seen This Algorithm Question (835 Views)
| Please Have You Seen This Algorithm Question by cbrass(op): 4:06pm On Oct 02, 2021 |
Write a production-ready function in PHP that sums the numbers in a file and outputs details of the results. The function will receive as input the path to a single file. Each line of the file will contain either a number or a relative path to another file. For each file processed, output the file path and the sum of all of the numbers contained both directly in the file and in any of the sub files listed in the file (and their sub files, etc).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(op): 11:40pm On Oct 02, 2021 |
stanliwise: function fileSum($file) {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(op): 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*. Modified: 10:03am 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(op): 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(op): 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(op): 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(op): 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(op): 3:31pm On Oct 04, 2021*. Modified: 4:11pm 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 |
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
Flutter Cross Platform With Nodejs Developer Needed • Any Oracle Jdeveloper In The House ? • Questions On Software Business