0
Help. Isogram Detector Challenge.
I'm suprisingly confused on what's the error. The last 'for loop' for comparing letters seems to be empty, hence, showing empty or 'no output' message. Please check my code: https://www.sololearn.com/coach/83?ref=app
5 Respuestas
+ 4
The code coach requirement is that the code should output false if duplicate characters found in a string else it should output true, but your code check only for the consecutive characters.
Also when using length() method of c++, use `unsigned int` or `size_t` instead of only int.
Hint:-declare a bool variable and set it's value false.
Set up a for loop and a nested for loop, they should run till the length of the string and there you can use a condition statement and check for duplicate characters, if duplicate characters found then set the bool value to true.Ultimately, use the bool variable to output the result.
+ 1
Gulshan Mahawar thanks for your big help. I passed all test cases except test #3 which is hidden. Please see revised code:
#include <iostream>
#include <string>
using namespace std;
int main() {
string word;
cin>>word;
//get size of word
size_t char_len = word.length();
char arr_ch[char_len];
//convert and store string into char
for(int i=0;i<=char_len;i++){
arr_ch[i] = word[i];
}
bool flag = false;
//check for isogram/repeating letters
for(int i=0;i<=char_len;i++){
for(int j=0;j<=char_len;j++){
//compare letters
if(arr_ch[j]==arr_ch[j+1]){
flag = true;
}
}
}
//return bool value into text
cout<<boolalpha<<flag;
return 0;
}
0
Oops. Sorry, I wasn't aware. Here's the code:
#include <iostream>
#include <string>
using namespace std;
int main() {
string word;
cin>>word;
char arr_ch[word.length()];
//convert and store string into char
for(int i=0;i<=word.length();i++){
arr_ch[i] = word[i];
}
//check if char array contains string
/*for(int i=0;i<=word.length();i++){
cout<<arr_ch[i];
}*/
bool flag = 0;
//check for isogram/repeating letters
for(int i=0;i<=word.length();i++){
//compare letters
if(arr_ch[i]==arr_ch[i+1]){
//no output. why?
cout<<arr_ch[i];
//flag=1;
}
}
/*string isogram;
if(flag==1){
isogram = "false";
}else{
isogram = "true";
}
cout<<isogram;*/
return 0;
}
0
It worked like a charm. Thank you very much for your detailed explanation and for revising the code.
0
import pygame
import random
# Initialize Pygame
pygame.init()
# Screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
# Player attributes
player_size = 50
player_color = BLACK
player_x = 50
player_y = SCREEN_HEIGHT - player_size - 10
player_velocity = 5
# Obstacle attributes
obstacle_width = 50
obstacle_height = 50
obstacle_color = RED
obstacle_velocity = 5
# Set up the display
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Endless Runner")
# Function to create obstacles
def create_obstacle():
return [SCREEN_WIDTH, SCREEN_HEIGHT - obstacle_height - 10]
# Game loop
running = True
clock = pygame.time.Clock()
obstacles = [create_obstacle()]
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Move the player
keys = pygame.key.get_pressed()
if keys[pygame.K_UP] and player_y > 0:
player_y -= player_v