locked
C# divide number in specific way RRS feed

  • Question

  • User-466819921 posted

    <p>this is my code which is not returning data as per my requirement. so please rectify my code which will give me desire output.</p>
    <pre class="prettyprint prettyprinted"><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> </span><span class="typ">Main</span><span class="pun">(</span><span class="kwd">string</span><span class="pun">[]</span><span class="pln"> args</span><span class="pun">)</span><span class="pln">
    </span><span class="pun">{</span><span class="pln">
    </span><span class="typ">List</span><span class="str">&lt;int&gt;</span><span class="pln"> test1 </span><span class="pun">=</span><span class="pln"> </span><span class="typ">DistributeInteger</span><span class="pun">(</span><span class="lit">10</span><span class="pun">,</span><span class="lit">1</span><span class="pun">).</span><span class="typ">ToList</span><span class="pun">().</span><span class="typ">OrderBy</span><span class="pun">(</span><span class="pln">s</span><span class="pun">=&gt;</span><span class="pln"> s</span><span class="pun">).</span><span class="typ">ToList</span><span class="pun">();</span><span class="pln">
    </span><span class="typ">List</span><span class="str">&lt;int&gt;</span><span class="pln"> test2 </span><span class="pun">=</span><span class="pln"> </span><span class="typ">DistributeInteger</span><span class="pun">(</span><span class="lit">10</span><span class="pun">,</span><span class="pln"> </span><span class="lit">2</span><span class="pun">).</span><span class="typ">ToList</span><span class="pun">().</span><span class="typ">OrderBy</span><span class="pun">(</span><span class="pln">s </span><span class="pun">=&gt;</span><span class="pln"> s</span><span class="pun">).</span><span class="typ">ToList</span><span class="pun">();</span><span class="pln">
    </span><span class="typ">List</span><span class="str">&lt;int&gt;</span><span class="pln"> test3 </span><span class="pun">=</span><span class="pln"> </span><span class="typ">DistributeInteger</span><span class="pun">(</span><span class="lit">10</span><span class="pun">,</span><span class="pln"> </span><span class="lit">3</span><span class="pun">).</span><span class="typ">ToList</span><span class="pun">().</span><span class="typ">OrderBy</span><span class="pun">(</span><span class="pln">s </span><span class="pun">=&gt;</span><span class="pln"> s</span><span class="pun">).</span><span class="typ">ToList</span><span class="pun">();</span><span class="pln">
    </span><span class="typ">List</span><span class="str">&lt;int&gt;</span><span class="pln"> test4 </span><span class="pun">=</span><span class="pln"> </span><span class="typ">DistributeInteger</span><span class="pun">(</span><span class="lit">10</span><span class="pun">,</span><span class="pln"> </span><span class="lit">4</span><span class="pun">).</span><span class="typ">ToList</span><span class="pun">().</span><span class="typ">OrderBy</span><span class="pun">(</span><span class="pln">s </span><span class="pun">=&gt;</span><span class="pln"> s</span><span class="pun">).</span><span class="typ">ToList</span><span class="pun">();</span><span class="pln">
    </span><span class="typ">List</span><span class="str">&lt;int&gt;</span><span class="pln"> test5 </span><span class="pun">=</span><span class="pln"> </span><span class="typ">DistributeInteger</span><span class="pun">(</span><span class="lit">10</span><span class="pun">,</span><span class="pln"> </span><span class="lit">5</span><span class="pun">).</span><span class="typ">ToList</span><span class="pun">().</span><span class="typ">OrderBy</span><span class="pun">(</span><span class="pln">s </span><span class="pun">=&gt;</span><span class="pln"> s</span><span class="pun">).</span><span class="typ">ToList</span><span class="pun">();</span><span class="pln">
    </span><span class="typ">List</span><span class="str">&lt;int&gt;</span><span class="pln"> test6 </span><span class="pun">=</span><span class="pln"> </span><span class="typ">DistributeInteger</span><span class="pun">(</span><span class="lit">10</span><span class="pun">,</span><span class="pln"> </span><span class="lit">6</span><span class="pun">).</span><span class="typ">ToList</span><span class="pun">().</span><span class="typ">OrderBy</span><span class="pun">(</span><span class="pln">s </span><span class="pun">=&gt;</span><span class="pln"> s</span><span class="pun">).</span><span class="typ">ToList</span><span class="pun">();</span><span class="pln">
    </span><span class="typ">List</span><span class="str">&lt;int&gt;</span><span class="pln"> test7 </span><span class="pun">=</span><span class="pln"> </span><span class="typ">DistributeInteger</span><span class="pun">(</span><span class="lit">10</span><span class="pun">,</span><span class="pln"> </span><span class="lit">7</span><span class="pun">).</span><span class="typ">ToList</span><span class="pun">().</span><span class="typ">OrderBy</span><span class="pun">(</span><span class="pln">s </span><span class="pun">=&gt;</span><span class="pln"> s</span><span class="pun">).</span><span class="typ">ToList</span><span class="pun">();</span><span class="pln">
    </span><span class="typ">List</span><span class="str">&lt;int&gt;</span><span class="pln"> test8 </span><span class="pun">=</span><span class="pln"> </span><span class="typ">DistributeInteger</span><span class="pun">(</span><span class="lit">10</span><span class="pun">,</span><span class="pln"> </span><span class="lit">8</span><span class="pun">).</span><span class="typ">ToList</span><span class="pun">().</span><span class="typ">OrderBy</span><span class="pun">(</span><span class="pln">s </span><span class="pun">=&gt;</span><span class="pln"> s</span><span class="pun">).</span><span class="typ">ToList</span><span class="pun">();</span><span class="pln">
    </span><span class="typ">List</span><span class="str">&lt;int&gt;</span><span class="pln"> test9 </span><span class="pun">=</span><span class="pln"> </span><span class="typ">DistributeInteger</span><span class="pun">(</span><span class="lit">10</span><span class="pun">,</span><span class="pln"> </span><span class="lit">9</span><span class="pun">).</span><span class="typ">ToList</span><span class="pun">().</span><span class="typ">OrderBy</span><span class="pun">(</span><span class="pln">s </span><span class="pun">=&gt;</span><span class="pln"> s</span><span class="pun">).</span><span class="typ">ToList</span><span class="pun">();</span><span class="pln">
    </span><span class="typ">List</span><span class="str">&lt;int&gt;</span><span class="pln"> test10 </span><span class="pun">=</span><span class="pln"> </span><span class="typ">DistributeInteger</span><span class="pun">(</span><span class="lit">10</span><span class="pun">,</span><span class="pln"> </span><span class="lit">10</span><span class="pun">).</span><span class="typ">ToList</span><span class="pun">().</span><span class="typ">OrderBy</span><span class="pun">(</span><span class="pln">s </span><span class="pun">=&gt;</span><span class="pln"> s</span><span class="pun">).</span><span class="typ">ToList</span><span class="pun">();</span><span class="pln">

    </span><span class="pun">}</span><span class="pln">

    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="typ">IEnumerable</span><span class="str">&lt;int&gt;</span><span class="pln"> </span><span class="typ">DistributeInteger</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> pagetotal</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> userinput</span><span class="pun">)</span><span class="pln">
    </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">userinput</span><span class="pun">==</span><span class="lit">0</span><span class="pun">)</span><span class="pln">
    </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">yield</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
    </span><span class="kwd">else</span><span class="pln">
    </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">int</span><span class="pln"> rest </span><span class="pun">=</span><span class="pln">pagetotal </span><span class="pun">%</span><span class="pln"> userinput</span><span class="pun">;</span><span class="pln">
    </span><span class="kwd">double</span><span class="pln"> result </span><span class="pun">=</span><span class="pln">pagetotal </span><span class="pun">/</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">double</span><span class="pun">)</span><span class="pln"> userinput</span><span class="pun">;</span><span class="pln">

    </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> i</span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">&lt;</span><span class="pln"> userinput</span><span class="pun">;</span><span class="pln">i</span><span class="pun">&#43;&#43;)</span><span class="pln">
    </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">rest</span><span class="pun">--</span><span class="pln"> </span><span class="pun">&gt;</span><span class="lit">0</span><span class="pun">)</span><span class="pln">
    </span><span class="kwd">yield</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">int</span><span class="pun">)</span><span class="pln"> </span><span class="typ">Math</span><span class="pun">.</span><span class="typ">Ceiling</span><span class="pun">(</span><span class="pln">result</span><span class="pun">);</span><span class="pln">
    </span><span class="kwd">else</span><span class="pln">
    </span><span class="kwd">yield</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">int</span><span class="pun">)</span><span class="pln"> </span><span class="typ">Math</span><span class="pun">.</span><span class="typ">Floor</span><span class="pun">(</span><span class="pln">result</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
    </span><span class="pun">}</span></pre>
    <p><strong>My requirement as following.</strong></p>
    <p><span>1) if total page is 10 and user split into 2 files then two files will be generated each file will have 5 pages</span></p>
    <p><span>2)&nbsp;</span><span>if total page is 10 and user split into 3 files then 3 files will be generated. first 2 files will have 3 pages and last pages will have 4 pages.</span></p>
    <p><span>3)&nbsp;</span><span>if total page is 10 and user split into 4 files then 4 files will be generated. first 3 files will have 3 pages and last pages will have 1 pages.</span></p>
    <p><span>4)&nbsp;</span><span>if total page is 10 and user split into 5 files then 5 files will be generated. each files will have 2 pages</span></p>
    <p><span>5)&nbsp;</span><span>if total page is 10 and user split into 6 files then first 5 files will be generated with 1 page. last file will have 5 pages</span></p>
    <p><span>6)&nbsp;</span><span>if total page is 10 and user split into 7 files then first 6 files will be generated with 1 page. last file will have 4 pages.</span></p>
    <p><span>7)&nbsp;</span><span>if total page is 10 and user split into 8 files then first 7 files will be generated with 1 page. last file will have 3 pages.</span></p>
    <p><span>8)&nbsp;</span><span>if total page is 10 and user split into 9 files then first 8 files will be generated with 1 page. last file will have 2 pages.</span></p>
    <p><span>8)&nbsp;</span><span>if total page is 10 and user split into 10 files then 10 files will be generated with 1 page.&nbsp;</span></p>
    <p><span><strong>again</strong></span></p>
    <p><span>1)&nbsp;</span><span>if total page is 9 and user split into 2 files then first file will be generated with 4 page. last file will have 5 pages.</span><span>2)&nbsp;</span><span>if total page is 9 and user split into 3 files then each 3 files will have 3 pages.</span></p>
    <p><span>3)&nbsp;</span><span>if total page is 9 and user split into 4 files then first 3 files will have 2 pages and last page will have 3 pages.</span></p>
    <p><span>4)&nbsp;</span><span>if total page is 9 and user split into 5 files then first 4 files will have 2 pages and last page will have 2 pages.</span><span>5)&nbsp;</span><span>if total page is 9 and user split into 6 files then first 5 files will have 1 pages and
    last page will have 4 pages.</span></p>
    <p><span>6)&nbsp;</span><span>if total page is 9 and user split into 7 files then first 6 files will have 1 pages and last page will have 3 pages.</span></p>
    <p><span>7)&nbsp;</span><span>if total page is 9 and user split into 8 files then first 7 files will have 1 pages and last page will have 2 pages.</span></p>
    <p><span>8)&nbsp;</span><span>if total page is 9 and user split into 9 files then each file will have 1 pages.</span></p>
    <p>hopefully i am clear what i am looking for. please give me working code which give right result as per my explanation at top point wise.</p>
    <p><b>The output i am getting from my program which is not correct some time. data given below.</b><br>
    <br>
    <span>Here i am mentioning what kind of output i am getting from above code and what kind of i am expecting.</span><br>
    <br>
    <span>1) List test4 = DistributeInteger(10, 4).ToList().OrderBy(s =&gt; s).ToList(); output is getting 2,2,3,3 but output should be 2,2,2,4</span><br>
    <br>
    <span>2) List test6 = DistributeInteger(10, 6).ToList().OrderBy(s =&gt; s).ToList(); output is getting 1,1,2,2,2,2 but output should be 1,1,1,1,1,5</span><br>
    <br>
    <span>3) List test7 = DistributeInteger(10, 7).ToList().OrderBy(s =&gt; s).ToList(); output is getting 1,1,1,1,2,2,2 but output should be 1,1,1,1,1,1,4</span><br>
    <br>
    <span>4) List test8 = DistributeInteger(10, 8).ToList().OrderBy(s =&gt; s).ToList(); output is getting 1,1,1,1,1,1,2,2 but output should be 1,1,1,1,1,1,1,3</span><br>
    <br>
    <span>5) List test9 = DistributeInteger(10, 9).ToList().OrderBy(s =&gt; s).ToList(); output is getting 1,1,1,1,1,1,1,1,2 but output should be 1,1,1,1,1,1,1,1,2</span><br>
    <br>
    <span>please guide me what to change in routine <strong>DistributeInteger()</strong> thanks</span></p>
    Friday, August 10, 2018 9:05 PM

Answers

  • User-1003897979 posted
    public IEnumerable<int> DistributeInteger(int pagetotal, int userInput)
    {
      return Enumerable.Repeat(pagetotal/userInput, userInput-1)
                       .Append(pagetotal-pagetotal/userInput*(userInput-1));
    }

    (PS - Get rid of all the .ToList and .OrderBy stuff.  Make the type of test1,2, … be IEnumerable<int>)

    (PPS - or better yet, just use var)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 10, 2018 11:23 PM

All replies

  • User-1003897979 posted
    public IEnumerable<int> DistributeInteger(int pagetotal, int userInput)
    {
      return Enumerable.Repeat(pagetotal/userInput, userInput-1)
                       .Append(pagetotal-pagetotal/userInput*(userInput-1));
    }

    (PS - Get rid of all the .ToList and .OrderBy stuff.  Make the type of test1,2, … be IEnumerable<int>)

    (PPS - or better yet, just use var)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 10, 2018 11:23 PM
  • User-466819921 posted


    I test code it worked but found no append function.
    So my request that please show me how to append last value ienumerable?

    I am using asp.net mvc 5.
    Thank a lot for your help.
    Saturday, August 11, 2018 3:54 PM
  • User-1003897979 posted

    go to google and type

    MSDN Append

    scan through the list of results to find the most likely one.  Click on it.  There you will see the required assembly and namespace.

    Sunday, August 12, 2018 2:25 AM
  • User-466819921 posted

    Append is there in. Net core. How to use append in asp. Net mvc5 ?

    If possible provide some sample code.

    i got one extension from https://stackoverflow.com/a/14265097/10094088

    public static IEnumerable<T> Append<T>(this IEnumerable<T> first, params T[] second)
    {
        return first.Concat(second);
    } 

    tell me how to use or fit above Append() extension in your code ? provide some sample code. thanks

    Sunday, August 12, 2018 2:48 PM
  • User-466819921 posted

    I got this formula

    for(int i=0; i<N-1; i++) yield return SUM/N;    // first N-1 numbers
    yield return SUM-SUM/N*(N-1);                   // making sure total equals SUM
    Sunday, August 12, 2018 4:14 PM