MountBlue Technologies Question asked on 23 January 2021.

1. How Many Words

A sentence is made up of a group of words. Each word is a sequence of letters, (a-z, A-Z), that may contain one or more hyphens and may end in a punctuation mark: period (.), comma (,), question mark (?), or exclamation point (!). Words will be separated by one or more white space characters. Hyphens join two words into one and should be retained while the other punctuation marks should be stripped. Determine the number of words in a given sentence.

Example

‘How many eggs are in a half-dozen, 13?’, The list of words in the string is [How ‘many ‘eggs’, ‘are’, in’, a, ‘half-dozen’] and the number of words is 7. Notice that the numeric string, not a word because it is not within the allowed character set.

Function Description:

Complete the function howMany in the editor.

howMany has the following parameter(s):

sentence: a string

Returns:

int: an integer that represents the number of words in the string.

Sample Input

he is a good programmer, he won 865 competitions, but sometimes he dont. What do you think? All test-cases should pass. Done-done?

Sample Output 21

Solution

#import re library
import re
def howMany(sentence):
# initialize total with 0
    total=0
# convert string into list 
    slist=list(sentence.split())
# iterate list one by one
    for i in slist:
find all valid character in element
        j=re.findall("[a-zA-Z*,*?*\-!*.]",i)
        # compare length of element with new element
        if len(i)==len(j):
           #increment total by one
            total+=1

    return total

2. Zig-Zag Array

Given an array of integers, change it in such a way – : that it follows a zig-zag pattern. A zig-zag array is one where for each Integer, Itsadjacent are both greater than or less than itself. In other is words, using L to mean a lower value and H to mean higher, the array follows either the pattern [L.H,LH…] or [H,L,H,L…]. To make the array a zig-zag array, you can replace any element with any other integer (positive, negative, zero). What is the 20 minimum number of replacements required to accomplish this?

Example : arr=[1, 2,3, 4,5] 

Original : [1 , 2 , 3 , 4 , 5]
LHLHL : [1 , 2 , – , 4 , – ]
HLHLH : [+ , 2 ,3 , – . 5 ]

To achieve an array starting with a low value, both the 3 and the 5 need to be reduced to any value less than 2 and 4 respectively. To achieve an array starting with a high value, the 1 needs to be increased (any value > 2) and the 4 needs to be decreased (any value < 3).

I n this case, creating either form of zig-zag array 5 takes a minimum of 2 replacements, the final.

Solution

def minOperation(arr):
    l1=list()
    l2=list()
    l1=arr
    l2=arr
    n=len(arr)
    maxs=max(arr)+1
    mins=min(arr)-1

    lh=0
    for i in range(1,n):
        if i%2!=0 and l1[i]<=l1[i-1]:
            lh+=1
            l1[i]=maxs
        elif i%2==0 and l1[i]>=l1[i-1]:
            lh+=1
            l1[i]=mins
    hl=0
    for i in range(1,n):
        if i%2!=0 and l1[i]>=l1[i-1]:
            hl+=1
            l2[i]=maxs
        elif i%2==0 and l1[i]<=l1[i-1]:
            hl+=1
            l2[i]=mins
    return min(lh,hl)

MountBlue

Manoj Kumar

I Love Technology and like to Explore new Technologies

This Post Has 3 Comments

  1. Varun

    Thank you Manoj, this helped!

Leave a Reply to Varun Cancel reply