In order to learn a language, the best way is to practise it! Thus, I have decided to challange myself to solve a task on CodeForces with VBScript. As far as VBScript is not one of the official CodeForces languages (no doubt about it), I thought it would be a good idea to solve the program in C# first and then change it to VBScript, thus making sure that it works as expected.
Top-model Izabella participates in the competition. She wants to impress judges and show her mathematical skills.
Her problem is following: for given string, consisting of only 0 and 1, tell if it’s possible to remove some digits in such a way, that remaining number is a representation of some positive integer, divisible by 64, in the binary numerical system.
In the only line given a non-empty binary string s with length up to 100.
Print «yes» (without quotes) if it’s possible to remove digits required way and «no» otherwise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
using System; using System.Linq; class Startup { static void Main() { string input = Console.ReadLine(); string newInput = ""; bool oneIsFound = false; for (int i = 0; i < input.Length; i++) { if (oneIsFound) { newInput += input[i]; } else { if (input[i] == '1') { oneIsFound = true; } } } int zeroesCount = newInput.Count(f => f == '0'); Console.WriteLine(zeroesCount > 5 ? "yes" : "no"); } } |
Now the VBScript. Let’s take a look how to “translate” it there.
First of all – open a text file and save it as “main.html”. Then the party starts. Actually, many browsers are somehow against VBScript. Probably for a reason. Thus, in order to see it running, you should do the following troubleshooting with Internet Explorer:
- Press F12 to open developer tools
- In left toolbar scroll down until you see “Emulation” settings page
- Change Document Mode from default (“Edge”) to 10
- Try using the following code
It’s pretty fun:
Some of the tricks with VBA tricks work there and once you start working even a small task with VBScript you somehow start liking and appreciating the VBEditor (which is not updated since more than 10+ years.)
At the end, this is the code of the CodeForces.Com task:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<html> <head> <meta http-equiv="x-ua-compatible" content="IE=10"> <title>VitoshAcademy VBScript</title> </head> <body> <script type="text/vbscript"> Option Explicit Dim oneIsFound, strInput,i, currentChar, newString, counter counter = 0 newString = "" oneIsFound = 0 strInput = "00000000000000111111111111000000000000000000000" document.write("VBScript example! :)") document.write(strInput) document.write("<br /><hr>") For i = 1 to Len(strInput) currentChar = Mid(strInput,i,1) If (oneIsFound = 1) Then If currentChar = 0 Then counter = counter + 1 End if newString = newString + currentChar Else If currentChar = 1 Then OneIsFound = 1 End if End If Next document.write("<br /><hr>") document.write(newString) document.write("<br /><hr>") if (counter>5) then document.write("Divisable by 64.") Else document.write("Not divisable by 64.") End if </script> </body> </html> |
The interesting part is that I even managed to do it with one loop and not with two as in the original problem. This is how it looks in the browser:
Pretty much that is it! The inspiration to try something today with VBScript came from the tutorial of guru99 for VBScript! It is quite interesting if you are fans of “old stuff”. If not, there are other tutorials on the site as well!
Cheers! 🙂