Justa Guy
Justa Guy

Reputation: 1

DCoder bitwise solution. As I can't see the test cases, it's difficult to understand what I did wrong

I'm using a website called dcoder.tech to help me test how well I use swift. They show you one test case and the results that your code should output. You write a program that takes input, the website generates more than one random sets of input, your program manipulates the input and prints the results, then dcoder judges your code based on how many cases you got right. Its console IO basically. My code printed the correct results for the test case they give you, but fails the other cases. I can't see the other cases or the results they give. DCoder is using Swift 5.0.1. I can give DCoder my own input, so I know the code doesn't crash. Here is the problem:

Problem Description: This is to provide you with an introductory experience of bit-wise operations. There are basically six bit-wise operations in any programming language : - AND(&), OR(|), XOR(^), NOT(~), left-shift operator(<<) and right-shift operator(>>). You will be given 2 numbers a and b to perform AND, OR and XOR operations and 2 number p and q to perform bit-wise shifting operations(left and right) and a number x to perform the NOT(~) operation. Print the output upon performing these operations each in a newline.

Input: five positive integers a, b, p, q and x separated by a space.

Output: In the three lines, print the output upon performing Bitwise AND, OR, XOR operations respectively on 'a' and 'b'. In the next two lines, print the output of left-shifting 'p' by 'q' bits and right-shifting 'p' by 'q' bits, respectively. In the last line, print the output upon performing the bit-wise NOT operation on 'x'.

Constraints: 0 ≤ a, b, p, q, x ≤ 255

Sample Input: 2 4 2 1 6

Sample Output: 0 6 6 4 1 -7

Here is my code. I'd like to point out that converting strings to ints in swift is stupid. All of the ways other sources gave me to do this didn't seem to work. Converting the string to an array with .split seems to be the only sane way to do this.

import Foundation

// This line is only uncommented for submission to DCoder
var myInput: String = readLine() ?? "No input"
// This line is only uncommented for use in playgrounds
// let myInput: String = "2 4 2 1 6"

let splitVariable = myInput.split(separator: " ")

let a = Int(splitVariable[0]) ?? 0
let b = Int(splitVariable[1]) ?? 0
let p = Int(splitVariable[2]) ?? 0
let q = Int(splitVariable[3]) ?? 0
let x = Int(splitVariable[4]) ?? 0

print(a & b)
print(a | b)
print(a ^ b)
print(p << q)
print(p >> q)
print(~x)

I tried to submit the code to DCoder. I was expecting that DCoder would run my code and say 'pass' I cannot inspect the output because DCoder only says pass or fail.

Upvotes: 0

Views: 38

Answers (0)

Related Questions