• Register
0 votes
609 views

Problem :

I am completely stuck on this problem

Given an array of ints, return True if the array contains a 2 next to a 2 somewhere.

has22([1, 2, 2])  True
has22([1, 2, 1, 2])  False
has22([2, 1, 2])  False

I know the basic idea but I am unable to implement it. I would also like to know that what type of the problem is this, eg. graph, search?

def has22(mynums):
for l in mynums:
    if ( (mynums[l] = 2) and (mynums[l+1] = 2) )
        return True
return False 
6 5 3
7,540 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

Please find below the required source code:

def has22(mynums):
    for j in range(len(mynums) - 1):
        if mynums[j] == 2 and mynums[j+ 1] == 2:
            return True
    return False

This was the easiest solution that I came up with.

Using the for loop to check if the iterated number, mynums[j] == 2 "and" the one very next to it, which is [j+1] == 2 as well.

Here (len(mynums)-1): this line will just prevent it from going out of the range through the for loop as the j+1 on the final loop will check out of the range.

 

I hope you have clearly understood the solution given to you by me.

9 7 4
38,600 points

Related questions

0 votes
1 answer 6 views
6 views
Problem: Can anyone help me out about this? If yes, then please answer me.
asked Mar 27 niyatiry 3.9k points
0 votes
1 answer 24 views
24 views
Problem: How to Check state of inputFile after opening and each inputFile >> numbers[count] operation separately to make sure there is no errors in loading from the file. Also consider what should happen if the file contained less than ARRAY_SIZE numbers. I would also suggest using std::array or std::vector which perform more checks in good debuggers.
asked Dec 22, 2020 Mashhoodch 13k points
0 votes
1 answer 30 views
30 views
Problem: return true if the string "cat" and "dog" appear the same number of times in the given string.
asked Feb 23 Muneeb Saadii 130k points
0 votes
2 answers 1K views
1K views
Problem : I am facing following issue need solution return true if the string "cat" and "dog" appear the same number of times in the given string
asked Nov 13, 2019 peterlaw 6.9k points
0 votes
1 answer 19 views
19 views
Problem: It’s an urgent Problem, any help: Assign the size of userinput to stringsize. ex: if userinput = "hello", output is
asked Mar 13 Wafa Abu Yousef 6.1k points
0 votes
1 answer 2.2K views
2.2K views
Problems : Assign the size of userInput to stringSize. Ex: if userInput = "Hello", output is: Size of userInput: 5 #include <stdio.h> #include <string.h> int main(void) { char userInputArray[50] = ""; int stringInputSize = 0; strcpy(userInputArray, "Hello"); /* Your solution starts here */ printf("Size of userInputArray: %d\n", stringInputSize); return 0; }
asked Dec 18, 2019 alecxe 7.5k points
0 votes
1 answer 10 views
10 views
Problem: I am trying to create a React-Native app and I need to connect my device to troubleshoot some issues that only occur on the physical device and not the simulator. For some reason, I have never been able to connect my Pixel 2 XL to my PC
asked Mar 31 ummesalma 29.2k points
0 votes
1 answer 10 views
10 views
Problem: I had a homework problem that was as follows:A 2-dimensional 3x3 array of ints, has been created and assigned to tictactoe. Write an expression whose value is true if the elements of the first row are all equal. I was just wondering why both of these solutions are correct as it seems like his is redundant and should be wrong.
asked Apr 26 Humaira ahmed 50.7k points
1 vote
1 answer 9 views
9 views
Problem: How to read line by line using C++. Reading from a file c++.
asked Mar 24 PkGuy 23.5k points