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

394 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 `

## 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.

## Related questions

6 views
Problem: Write a Python program to get next day of a given date.Date is provided as day, month and year The input provided is always valid. Output should be day-month-year. Hint: print(day,"-",month,"-",year) will display day-month-yearAlso identify the test data and use it to test the program.
691 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
1.7K 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; }
8 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.
10 views
Problem: I have been learning C# and got an assignment in which I have to read a string as input from a user and then check if it contains a digit or not. If it contains a digit, a bool variable must be set to true, or else it must remain false. After this, a validity ... of the bool variable. I have no idea how I can do this task. If any of you could help, that would be a great pleasure. Thanks!
166 views
Problem: I am new in programming and stuck in a query, can anyone guide me? Write an expression that evaluates to True if the str associated with s starts with "p"?
1 vote
684 views
Problem: Hello Pal, I have a query regarding the python programming language. The question is, which method will return an empty string when it has attempted to read beyond the end of a file? Do you have any idea about this? Thanks
19 views
Problem: Given the array a, write an expression that refers to the first element of the array?
1 vote
736 views
Problem: I am new in python programming and I have been doing a course on it as a new comer I tried to repeat some char in a string. I wrote a code : def Work(str): for i in range(len(str)): return i * 2 But it does not work . how can I do this?