Simple Programming
30 points Easy

Can you help me? I need to know how many lines there are where the number of 0's is a multiple of 3 or the numbers of 1s is a multiple of 2. Please! Here is the file: https://mega.nz/#!7aoVEKhK!BAohJ0tfnP7bISIkbADK3qe1yNEkzjHXLKoJoKmqLys

Flag
Rating 4.41
5
4
3
2
1

Discussion

it took me three months to count it all....

0

it took me three months to count it all....

0

it took me three months to count it all....

0

it took me three months to count it all....

0

it took me three months to count it all....

0

it took me three months to count it all....

0

it took me three months to count it all....

0
Protected
1

Open the file

with open("data.dat", 'r') as file: # Read all lines from the file lines = file.readlines()

Initialize a counter for lines meeting the conditions

count = 0

Iterate through each line in the list of lines

for line in lines: # Strip whitespace from the line and check if it's not empty if line.strip(): # Count the occurrences of '0' and '1' in the line count_0 = line.count('0') count_1 = line.count('1')

    # Check if the count of 0's is a multiple of 3 or the count of 1's is a multiple of 2
    if count_0 % 3 == 0 or count_1 % 2 == 0:
        # Increment the counter if the condition is met
        count += 1

Print the count of lines meeting the conditions

print(count)

1

Remember OR is important, this will impact the logic of ur code, ez challenge

1

I used regex with node.js ```const fs = require('fs') var text = fs.readFileSync('./data.dat','utf8')

function count0(bin){ var arr = bin.match(/(0)/g); return arr ? arr.length : 0; } function count1(bin){ var arr = bin.match(/(1)/g); return arr ? arr.length : 0; }

var binArray = text.split('\n') var succArray = []

binArray.forEach((bin) => { var zeros = count0(bin); var ones = count1(bin); if(ones % 2 == 0){ succArray.push(bin); } else if(zeros % 3 == 0){ succArray.push(bin); } }); console.log(succArray.length)```

0