1 2 3 4 5 6 7 8 9 10 Next »

58. Length of Last Word

Create Date: May 29, 2019 at 10:35 PM         Tag: PYTHON         Author Name: Sun, Charles

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.

If the last word does not exist, return 0.

Note: A word is defined as a character sequence consists of non-space characters only.

Example:

Input: "Hello World"
Output: 5

java:

//0ms
class Solution {
    public int lengthOfLastWord(String s) {
        return s.trim().length() - s.trim().lastIndexOf(" ") - 1;
    }
}
//1ms
class Solution {
    public int lengthOfLastWord(String s) {
        if (s == null) {
            return 0;
        }
        s = s.trim();
        if (s.isEmpty()) { return 0; }
        String[] res = s.split(" ");
        return res[res.length - 1].length();
    }
}
//0ms
class Solution {
    public int lengthOfLastWord(String s) {
        int len = 0, tail = s.length() - 1;
        char[] c = s.toCharArray();
        while (tail >= 0 && c[tail] == ' ') tail--;
        while (tail >= 0 && c[tail] != ' ') {
            len++;
            tail--;
        }
        return len;
    }
}

 

New Comment

Default Arguments

Create Date: April 01, 2019 at 10:25 PM         Tag: PYTHON         Author Name: Sun, Charles

In this challenge, the task is to debug the existing code to successfully execute all provided test files.


Python supports a useful concept of default argument values. For each keyword argument of a function, we can assign a default value which is going to be used as the value of said argument if the function is called without it. For example, consider the following increment function:

def increment_by(n, increment=1):
    return n + increment

The functions works like this:

>>> increment_by(5, 2)
7
>>> increment_by(4)
5
>>>

Debug the given function print_from_stream using the default value of one of its arguments.

The function has the following signature:

def print_from_stream(n, stream)

This function should print the first  values returned by get_next() method of stream object provided as an argument. Each of these values should be printed in a separate line.

Whenever the function is called without the stream argument, it should use an instance of EvenStream class defined in the code stubs below as the value of stream.

Your function will be tested on several cases by the locked template code.

Input Format

The input is read by the provided locked code template. In the first line, there is a single integer  denoting the number of queries. Each of the following  lines contains a stream_name followed by integer , and it corresponds to a single test for your function.

Constraints

Output Format

The output is produced by the provided and locked code template. For each of the queries (stream_name, n), if the stream_name is even then print_from_stream(n) is called. Otherwise, if the stream_name is odd, then print_from_stream(n, OddStream()) is called.

Sample Input 0

3
odd 2
even 3
odd 5

Sample Output 0

1
3
0
2
4
1
3
5
7
9

Explanation 0

There are  queries in the sample.

In the first query, the function print_from_stream(2, OddStream()) is exectuted, which leads to printing values  and  in separated lines as the first two non-negative odd numbers.

In the second query, the function print_from_stream(3) is exectuted, which leads to printing values  and  in separated lines as the first three non-negative even numbers.

In the third query, the function print_from_stream(5, OddStream()) is exectuted, which leads to printing values and  in separated lines as the first five non-negative odd numbers.

class EvenStream(object):
    def __init__(self):
        self.current = 0

    def get_next(self):
        to_return = self.current
        self.current += 2
        return to_return

class OddStream(object):
    def __init__(self):
        self.current = 1

    def get_next(self):
        to_return = self.current
        self.current += 2
        return to_return

def print_from_stream(n, stream=EvenStream()):
    stream.__init__()
    for _ in range(n):
        print(stream.get_next())


queries = int(input())
for _ in range(queries):
    stream_name, n = input().split()
    n = int(n)
    if stream_name == "even":
        print_from_stream(n)
    else:
        print_from_stream(n, OddStream())

 

New Comment

Detect Floating Point Number

Create Date: April 01, 2019 at 09:51 PM         Tag: PYTHON         Author Name: Sun, Charles

Check Tutorial tab to know how to to solve.

You are given a string . 
Your task is to verify that  is a floating point number.

In this task, a valid float number must satisfy all of the following requirements:

 Number can start with +, - or . symbol. 
For example: 
✔+4.50 
✔-1.0 
✔.5 
✔-.7 
✔+.4 
✖ -+4.5

 Number must contain at least  decimal value. 
For example: 
✖ 12. 
✔12.0  

 Number must have exactly one . symbol. 
 Number must not give any exceptions when converted using .

Input Format

The first line contains an integer , the number of test cases. 
The next  line(s) contains a string .

Constraints

Output Format

Output True or False for each test case.

Sample Input 0

4
4.0O0
-1.00
+4.54
SomeRandomStuff

Sample Output 0

False
True
True
False

Explanation 0

: O is not a digit. 
: is valid. 
: is valid. 
SomeRandomStuff: is not a number.

method 1:

import re
[print(True if all(re.search(r, s) for r in [r"^[-+]?[0-9]*\.[0-9]+$"]) else False) for s in [input() for _ in range(int(input()))]]

method 2:

import re
for _ in range(int(input())):
    print(bool(re.match(r"^[-+]?[0-9]*\.[0-9]+$", input())))

 

New Comment
1 2 3 4 5 6 7 8 9 10 Next »