C #3.0

Source: Internet
Author: User

Transferred from Lao Zhao blog: http://www.cnblogs.com/JeffreyZhao/archive/2009/02/01/1381867.html

For more information about the citation, see: Learning Python from the textbook of CSDN.

Two algorithmic questions:

  1. It is required to calculate the number of different characters in a string of any length.
  2. Output a string in the ancient format of "right-up vertical bar.

The quote uses Python to solve this issue, which is very interesting and suitable for Python. However, it seems that this article does not really understand how C # Can be used today. When Zhao started, he tried to do it.

1st questions

Python version:

dic = {}for s in "abcdefgabc":    dic[s] = 1 if s not in dic else (dic[s]+1)print '\n'.join('%s,%s' % (k, v) for k, v in dic.items())

C #3.0

var values = "abcdefgabc"    .GroupBy(c => c)    .Select(g => String.Format("{0}, {1}", g.Key, g.Count()))    .ToArray();Array.ForEach(values, v => Console.WriteLine(v));

If an extension is added, you should know it at a Glance. Lao Zhao strongly recommends that you add the appropriate extension in the project-and then reduce the number of vertices:

"abcdefgabc"    .GroupBy(c => c)    .ForEach(v => Console.WriteLine(g.Key + ", " + g.Count()));

Or use "narrow-sense" LINQ?

string text = "abcdefgabc";var result = from c in text group c by c into g select g;foreach g in result Console.WriteLine(g.Key + "," + g.Count());
2nd questions

Python version:

Def main (offset = 6): string = u'wait for night, Li Bai, bed, bright moonlight, suspected to be on the ground cream. Look up at the moon and look down at your hometown. 090131 'a = [[''] * offset for row in xrange (offset)] for I in xrange (offset): for j in xrange (offset ): a [I] [j] = string [j + I * offset] B = [[r [col] for r in [:: -1] for col in xrange (len (a [0])] print '\ n '. join ([u'hangzhou '. join (unicode (c) for c in row) for row in B])

C # version 3.0:

static void Print(string text, int offset){    var matrix = text        .Select((c, i) => new { Char = c, Index = i })        .GroupBy(c => c.Index % offset, c => c.Char.ToString());    Array.ForEach(        matrix.ToArray(),        a => Console.WriteLine(String.Join("|", a.Reverse().ToArray())));}

C #3.0 extended version:

static void Print(string text, int offset){    text        .Select((c, i) => new { Char = c, Index = i })        .GroupBy(c => c.Index % offset, c => c)        .ForEach(g => Console.WriteLine(g.Reverse().Join("|")));}

Call:

Print ("the moonlight in front of the bed is suspected of being on the ground, and the frost raises his head and looks down at his hometown", 5 );

Result:

Low | moderate | suspicious | bedside | HEAD | similar | forward | hope | Ground | Ming | Shang | Yue Xiang | month | Shuang | Guang

Finally, I declare that this article is not intended to provoke a language competition. Python is a powerful and flexible language, and it has no harm in learning Python. Another benefit of Python is the code format. At least indentation is very standard-there is no way. In Python, indentation is part of the syntax. If indentation is incorrect, the Code cannot run. If the code format is not good, try again.

I hope that you can use a tool to make good use of it. The advanced features in C #3.0 are also an important tool to greatly improve development efficiency. In fact, the most important part of this "interesting programming" is "language features", "syntax", rather than the class library functions contained in it. In fact, Java also has an inline writing method, which can be written very short. It requires more than 20 lines of Java-it just doesn't know how to "use" Java.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.