September 04, 2015 Re: reading file byLine | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Edwin van Leeuwen | On Friday, 4 September 2015 at 12:06:08 UTC, Edwin van Leeuwen wrote:
> On Friday, 4 September 2015 at 11:50:23 UTC, deed wrote:
>>
>> import std.algorithm, std.range, std.array, std.string, std.stdio,
>> std.conv;
>>
>> int[] arr1 = [1, 2, 30];
>> //arr1.max.writeln; // Doesn't work, as you say
>> arr1.reduce!max.writeln; // This does. Prints 30.
>
> Again using reduce is the functional way to do it. The above basically boils down to:
>
> int[] arr1 = [1, 2, 30];
> int maxElement = arr1[1];
> foreach( element; arr1[2..$] ) //2..$ is short hand for second till last ($) element
> {
> maxElement = max( maxElement, element );
> }
> writeln( maxElement );
Sorry been using too much R, so my indexes are off by 1:
int[] arr1 = [1, 2, 30];
int maxElement = arr1[0];
foreach( element; arr1[1..$] ) //1..$ is short hand for second till last ($) element
{
maxElement = max( maxElement, element );
}
writeln( maxElement );
| |||
September 05, 2015 Re: reading file byLine | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Edwin van Leeuwen | On Friday, 4 September 2015 at 12:09:19 UTC, Edwin van Leeuwen wrote: > On Friday, 4 September 2015 at 12:06:08 UTC, Edwin van Leeuwen wrote: >> On Friday, 4 September 2015 at 11:50:23 UTC, deed wrote: >>> >>> import std.algorithm, std.range, std.array, std.string, std.stdio, >>> std.conv; >>> >>> int[] arr1 = [1, 2, 30]; >>> //arr1.max.writeln; // Doesn't work, as you say >>> arr1.reduce!max.writeln; // This does. Prints 30. >> >> Again using reduce is the functional way to do it. The above basically boils down to: >> >> int[] arr1 = [1, 2, 30]; >> int maxElement = arr1[1]; >> foreach( element; arr1[2..$] ) //2..$ is short hand for second till last ($) element >> { >> maxElement = max( maxElement, element ); >> } >> writeln( maxElement ); > > Sorry been using too much R, so my indexes are off by 1: > > int[] arr1 = [1, 2, 30]; > int maxElement = arr1[0]; > foreach( element; arr1[1..$] ) //1..$ is short hand for second till last ($) element > { > maxElement = max( maxElement, element ); > } > writeln( maxElement ); Thx guys. Now I try out the split function. I read the file as a single string? auto arr = split(cast(string)read(filename),","); where the file has "A", "B", "C" and I get the output ["\"A\"", " \"B\"", " \"C\"\n"] I can understand that read functions reads the endl but what does it with the quotation marks? how can I modify read so I get just ["A", "B", "C"] | |||
September 05, 2015 Re: reading file byLine | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Namal | On Saturday, 5 September 2015 at 12:41:37 UTC, Namal wrote:
> Thx guys. Now I try out the split function. I read the file as a single string?
>
> auto arr = split(cast(string)read(filename),",");
>
> where the file has "A", "B", "C"
>
> and I get the output ["\"A\"", " \"B\"", " \"C\"\n"]
>
> I can understand that read functions reads the endl but what does it with the quotation marks? how can I modify read so I get just ["A", "B", "C"]
'\' is the escape character and is used to disambiguate start or end of string (") and a quotation mark within the string (\"), the same way as "\n" means newline and not '\' 'n', which would have been "\\n".
So what you have is [`"A"`, ` "B"`, ` "C"\n`], if you use ` for start\stop of string. You say you want ["A", "B", "C"], so you need to remove whitespace. You can do that with std.string.strip. Assuming you also want to remove the quotation marks present in the file, one solution is to use std.string.chomp and std.string.chompPrefix, for example:
string s = cast(string) read(filename);
s.split(",")
.map!strip
.map!(s => chomp(s, "\"")
.map!(s => chompPrefix(s, "\"")
.writeln
;
| |||
September 05, 2015 Re: reading file byLine | ||||
|---|---|---|---|---|
| ||||
Posted in reply to deed | On Saturday, 5 September 2015 at 14:44:19 UTC, deed wrote:
> .map!(s => chomp(s, "\"")
> .map!(s => chompPrefix(s, "\"")
should be
.map!(s => chomp(s, "\""))
.map!(s => chompPrefix(s, "\""))
| |||
September 05, 2015 Re: reading file byLine | ||||
|---|---|---|---|---|
| ||||
Posted in reply to deed | On Saturday, 5 September 2015 at 14:49:13 UTC, deed wrote: > On Saturday, 5 September 2015 at 14:44:19 UTC, deed wrote: >> .map!(s => chomp(s, "\"") >> .map!(s => chompPrefix(s, "\"") > > should be > > .map!(s => chomp(s, "\"")) > .map!(s => chompPrefix(s, "\"")) Yeah, I have have been trying this example from wiki books https://en.wikibooks.org/wiki/Learning_D_With_Project_Euler It is not even compiling. | |||
September 05, 2015 Re: reading file byLine | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Namal | On Saturday, 5 September 2015 at 17:31:39 UTC, Namal wrote:
> Yeah, I have have been trying this example from wiki books
>
> https://en.wikibooks.org/wiki/Learning_D_With_Project_Euler
>
> It is not even compiling.
What exactly is not compiling?
| |||
September 05, 2015 Re: reading file byLine | ||||
|---|---|---|---|---|
| ||||
Posted in reply to deed | On Saturday, 5 September 2015 at 18:57:52 UTC, deed wrote:
> On Saturday, 5 September 2015 at 17:31:39 UTC, Namal wrote:
>> Yeah, I have have been trying this example from wiki books
>>
>> https://en.wikibooks.org/wiki/Learning_D_With_Project_Euler
>>
>> It is not even compiling.
>
> What exactly is not compiling?
the last codesample on the bottom. I think because of the old D? Index for the last array.length-1 is now $-1. But also I get
Error: undefined identifier 'file'
for the read line. But even when I fixed those errors the strings I got were with those quotation marks and backslashes. However, with your help I could solve it now.
I moved to the next problem and wrote the program for it
import std.stdio, std.algorithm, std.array;
bool abundant(int n){
int[] a;
foreach(i;1..n)
if(!(n%i))
a~=i;
auto sum = reduce!((a,b)=>a+b)(0,a);
return sum>n;
}
void main(){
long sum;
int[] arr;
int[28123] mark;
foreach(i;1..28124)
if(abundant(i))
arr~=i;
foreach(i;arr)
foreach(j;arr){
if(i+j>28123)
break;
mark[i+j-1] = 1;
}
for(auto i = 0;i<mark.length;++i)
if(!mark[i])
sum+=i+1;
writeln(sum);
}
How can I generate the array arr the functional way with reduce and the function abundant as filter. Tried it on my own but failed. Also, if I use reduce on an array, like I did for the sum, what does the zero mean in the range?
| |||
September 05, 2015 Re: reading file byLine | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Namal | On Saturday 05 September 2015 22:22, Namal wrote: > import std.stdio, std.algorithm, std.array; > > bool abundant(int n){ [...] > } > > > > void main(){ > > long sum; > int[] arr; > int[28123] mark; > > foreach(i;1..28124) > if(abundant(i)) > arr~=i; [...] > } > > How can I generate the array arr the functional way with reduce and the function abundant as filter. Tried it on my own but failed. import std.range: iota; int[] arr = iota(1, 28124).filter!abundant.array; > Also, if I use reduce on an array, like I did for the sum, what does the zero mean in the range? You mean the 0 in this: `reduce!((a,b)=>a+b)(0,a)`, right? It's used as the initial value for the `a` argument to the lambda. That is, the first calculation there is 0+a[0]. Then a[1] is added to that, and so on. Note that there's a specialized `std.algorithm.iteration.sum`. | |||
September 06, 2015 Re: reading file byLine | ||||
|---|---|---|---|---|
| ||||
Posted in reply to anonymous | > Note that there's a specialized `std.algorithm.iteration.sum`.
is there any function that removes double elements in a sorted array?
| |||
September 06, 2015 Re: reading file byLine | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Namal | On Sunday, 6 September 2015 at 15:41:34 UTC, Namal wrote: > is there any function that removes double elements in a sorted array? std.algorithm.iteration.uniq http://dlang.org/phobos/std_algorithm_iteration.html#uniq | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply