static class StringUtilities { public static int WordCount(this string text) { return text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Length; } } var text = "This is an example"; var count = text.WordCount();
@pysnooper.snoop() deffindPrimes(number): num = 2 primes = [] while num <= number: div = 2 flag = True while div * div <= num and flag: if num % div == 0: flag = False div = div + 1 if flag: primes.insert(len(primes), num) num = num + 1 return primes print(findPrimes(10))
@pysnooper.snoop("./debug1.log") defisPrime(num): div = 2 while div * div <= num: if num % div == 0: returnFalse div = div + 1 returnTrue
@pysnooper.snoop("./debug.log") deffindPrimes(number): num = 2 primes = [] while num <= number: if isPrime(num): primes.insert(len(primes), num) num = num + 1 return primes print(findPrimes(10))
@pysnooper.snoop("./debug.log", prefix="--isPrime--") defisPrime(num): div = 2 while div * div <= num: if num % div == 0: returnFalse div = div + 1 returnTrue
@pysnooper.snoop("./debug.log", prefix="--findPrimes--") deffindPrimes(number): num = 2 primes = [] while num <= number: if isPrime(num): primes.insert(len(primes), num) num = num + 1 return primes print(findPrimes(10))
defisPrime(num): div = 0 while div * div <= num: if num % div == 0: returnFalse div = div + 1 returnTrue
deffindPrimes(number): num = 2 primes = [] while num <= number: if isPrime(num): primes.insert(len(primes), num) num = num + 1 return primes print(findPrimes(10))
執行之後產生以下的 Crash info
1 2 3 4 5 6 7 8
Traceback (most recent call last): File "a.py", line 20, in <module> print(findPrimes(10)) File "a.py", line 16, in findPrimes if isPrime(num): File "a.py", line 7, in isPrime if num % div == 0: ZeroDivisionError: integer division or modulo by zero
defisPrime(num): div = 0 while div * div <= num: if num % div == 0: returnFalse div = div + 1 returnTrue
deffindPrimes(number): num = 2 primes = [] while num <= number: if isPrime(num): primes.insert(len(primes), num) num = num + 1 return primes print(findPrimes(10))
File a.py, line 20, in <module> 16 if isPrime(num): 17 primes.insert(len(primes), num) 18 num = num + 1 19 return primes --> 20 print(findPrimes(10))
File a.py, line 16, in findPrimes 12 def findPrimes(number): 13 num = 2 14 primes = [] 15 while num <= number: --> 16 if isPrime(num): 17 primes.insert(len(primes), num) .................................................. number = 10 num = 2 primes = [] ..................................................
File a.py, line 7, in isPrime 4 def isPrime(num): 5 div = 0 6 while div * div <= num: --> 7 if num % div == 0: 8 return False .................................................. num = 2 div = 0 ..................................................
ZeroDivisionError: integer division or modulo by zero
intmain(){ int i = 0; for (int z = 1; true; ++z) { for (int x = 1; x < z; ++x) { for (int y = x; y < z; ++y) { if (x * x + y * y == z * z) { printf("(%d,%d,%d)\n", x, y, z); if (++i == 10) goto done; } } } } done:; }
template<classF> voidgenerate_triples(Ff) { for (int z = 1; true; ++z) { for (int x = 1; x < z; ++x) { for (int y = x; y < z; ++y) { if (x*x + y * y == z * z) { bool stop = boolify(f)(std::make_tuple(x, y, z)); if (stop) return; } } } } }
template<classF> voidgenerate_triples(Ff) { for (int z = 1; true; ++z) { for (int x = 1; x < z; ++x) { for (int y = x; y < z; ++y) { if (x*x + y * y == z * z) { bool stop = boolify(f)(std::make_tuple(x, y, z)); if (stop) return; } } } } }
auto triples() -> generator<std::tuple<int, int, int>> { for (int z = 1; true; ++z) { for (int x = 1; x < z; ++x) { for (int y = x; y < z; ++y) { if (x*x + y * y == z * z) { co_yieldstd::make_tuple(x, y, z); } } } } }
intmain(){ auto g = triples(); for (int i = 0; i < 10; ++i) { g.move_next(); auto triple = g.current_value(); std::cout << '(' << std::get<0>(triple) << ',' << std::get<1>(triple) << ',' << std::get<2>(triple) << ')' << '\n'; } }
// A sample standard C++20 program that prints // the first N Pythagorean triples. #include<iostream> #include<optional> #include<range/v3/all.hpp> usingstd::get; usingstd::optional; usingstd::make_tuple; usingstd::cout; using ranges::view_interface; using ranges::iterator_t; namespace view = ranges::v3::view;
// maybe_view defines a view over zero or one // objects. template<classT> structmaybe_view : view_interface<maybe_view<T>> { maybe_view() = default; maybe_view(T t) : data_(std::move(t)) { } T const *begin()constnoexcept{ return data_ ? &*data_ : nullptr; } T const *end()constnoexcept{ return data_ ? &*data_ + 1 : nullptr; } private: optional<T> data_{}; }; // "for_each" creates a new view by applying a // transformation to each element in an input // range, and flattening the resulting range of // ranges. // (This uses one syntax for constrained lambdas // in C++20.) inlineconstexprauto for_each = [](auto&& r, auto fun) { returndecltype(r)(r) | view::transform(std::move(fun)) | view::join; }; // "yield_if" takes a bool and a value and // returns a view of zero or one elements. inlineconstexprauto yield_if = [](bool b, auto x) { return b ? maybe_view{std::move(x)} : maybe_view<decltype(x)>{}; }; intmain(){ // Define an infinite range of all the // Pythagorean triples: using view::iota; auto triples = for_each(iota(1), [](int z) { return for_each(iota(1, z+1), [=](int x) { return for_each(iota(x, z+1), [=](int y) { return yield_if(x*x + y*y == z*z, make_tuple(x, y, z)); }); }); }); // Display the first 10 triples for(auto triple : triples | view::take(10)) { cout << '(' << get<0>(triple) << ',' << get<1>(triple) << ',' << get<2>(triple) << ')' << '\n'; } }
$ cargo build error: the listed checksum of `/yamux/vendor/tokio-io/src/framed_read.rs` has changed: expected: 07e36ff58fe30bf7b0aa28a998b0bc4e2f78acd04cc1570a877b4eeac1cadcf7 actual: d10d30e1bc1f1d1abc7c28db97fd37ee374d029329aaa78df328bb076163363d
directory sources are not intended to be edited, if modifications are required then it is recommended that [replace] is used with a forked copy of the source