How to separate a string in commodore 64 basic? - basic

I have a board of "."s initialized into a board in commodore 64.
I want to randomly place words into the board with each letter of the word being a "." on the board (like a word search game). If the word does not fit, then the next word can be placed. I want to place the words vertically and horizontally. This is what I have so far: (this makes board of dots 10x10)
Any ideas on separating a word (I have the words hard coded) and placing them vertically and horizontally on the screen?
1060 rem: Subroutine Fill
1070 rem: Purpose: read and data construct which fills b3$(x,x) with
1080 rem: either "."s or other random words depending on whether or not
1090 rem: the subroutine has been run before.
1100 x = 10
1110 rem: x represents the dimension for the board; in this case, 10
1120 rem: took out dim b3$(x, x)
1130 rem: array b3 = board = specifications for width and height (10)
1140 rem: i to x allows the horizontal aspect of board to be filled with "."s
1150 for i = 0 to x
1160 rem: j to x allows the vertical aspect of board to be filled with "."s
1170 for j = 0 to x
1180 rem: board filled with dots horizontally and vertically
1190 b3$(i, j) = "."
1200 rem: end of first nested for loop
1210 next
1220 rem: end of second nested for loop
1230 next
1240 return
1400 dim wo$(9)
1410 wo$(0) = "word"
1420 wo$(1) = "stack"
1430 wo$(2) = "overflow"
1440 wo$(3) = "hello"
1450 wo$(4) = "no"
1460 wo$(5) = "how"
1470 wo$(6) = "why"
1480 wo$(7) = "start"
1490 wo$(8) = "end"
1500 wo$(9) = "done"
1510 print wo$(7)
1520 return
10 print "START"
20 rem: go to line 1100 in order to fill board with "."s because this is
30 rem: the board's initialization
40 gosub 1100
50 rem: looping from i to x allows for horizontal aspect of board to be printed
60 rem: x represents the width dimension of board, in this case, 10
70 for i = 0 to x
80 rem: looping from j to x allows for vertical aspect of board to be printed
90 rem: x represents the height dimension of board, in this case, 10
100 for j = 0 to x
110 rem: board initialized with "."s is printed
120 print b3$(i,j),
130 rem: end of first for loop, looping from i to x put on 130; , USED 4 TAB
140 next
150 print
160 rem: end of second for loop, looping from j to x
170 next
180 rem: checks what at the random number is equal to; places word vertically
190 rem: if rand is 0 and places the word horizontally if rand is 1
Now I need to place the words in the grid
Any ideas?

The MID$ String Function
Another vital function is MID$. This function selects a portion of any string it is given for its argument.
Type the command:
PRINT MID$("ABCOEFG",2,4)
The result shows you how MID$ works. In this case it displays a 4 character string, starting at the 2nd character of "ABCDEFG".
In formal terms, the MID$ function takes three arguments which are separated by commas and enclosed in brackets. The arguments are as
follows:
The first is the string to be used.
The second is a number specifying the position of the first character
in the result.
The third is another number giving the length of the result.
As you would expect, any of the arguments can be variables of the appropriate sort. The length of the result can be anything from 0 (called the null string) to the full length of the first argument. In practice it is often one character.
Here is a simple program to input a word and display it backwards. Study it carefully and note how the functions LEN and MID$ are used:
10 INPUT "PLEASE TYPE A WORD"; X$
20 PRINT "YOUR WORD BACKWARD IS"
30 FOR J = LEN(X$) TO 1 STEP - 1
40 PRINT MID$(X$,J, 1);
50 NEXT J
60 STOP
Key the program in and check it for yourself; try out words of 1, 2 or more characters.

Related

Given a binary string "10110". Find the count of all the substring with number of set bit count >= n

We could solve this question in brute force, taking all possible substrings and checking if the set bit count is greater n.
I was asked to solve this in o(n). I could not find any answer which could achieve this in o(n).
Is it possible to get all possible substrings of binary string in 0(n)?
Answer changed (noticed >= in problem statement).
Make two indices - left and right.
We want to account substrings starting from position left containing at least k ones.
At first move right until bit count reaches k.
Now we have some "good" substrings starting at left and ending in any position after right, so we can add len(s) - right + 1 to result.
Increment left by 1 until the next one.
Repeat moving right and so on. Algorithm is linear.
Python example:
s = '0010110010'
#s = '110010110010'
k = 2
left = 0
right = 0
res = 0
cnt = 0
while right < len(s):
while (right < len(s)) and (cnt < k):
if s[right] == "1":
cnt += 1
right +=1
while (left <= right) and (cnt >= k):
addend = len(s) + 1 - right
res += addend
print(left, right, addend, res) #intermediate debug output
if s[left] == "1":
cnt -= 1
left +=1
print(res)
0 5 6 6
1 5 6 12
2 5 6 18
3 6 5 23
4 6 5 28
5 9 2 30
30
A useful approach is to ask yourself how many substrings have less than n bits set.
If you can answer this question, then the answer to the original question is right around the corner.
Why is the modified question easier to grasp? Because when you have a substring, say S, with exactly n bits set, then any substring that contains S will have at least n bits set, so you don't need to examine any of those.
So let's say you have a substring. If it has less than n bits set, you can grow it to accommodate more bits. If it has n or more bits set, it cannot grow, you must shrink it.
Suppose you start from the leftmost empty substring, start index 0, end index 0, length 0. (Of course it's a half-open interval). It has no bits set, so you can grow it. The only direction it can grow is to the right, by increasing its end index. It grows and grows and grows until it eats n 1-bits; now it must shrink. How should it shrink? Obviously shrinking it in the opposite direction (decreasing its end index) would accomplish nothing. You would arrive at a substring you have just examined! So you should shrink it from the left, by increasing its start index. So it shrinks and shrinks and shrinks until it excretes a 1-bit from its rear end. Now it has n-1 1-bits, and it can grow again.
It is not difficult to show that you would enumerate all strings with less than n bits set this way.
let N = count of '1'
And let M = count of '0'
int sum = 0 ;
for( int i = n ; i <= N; i++ ) sum += C(N,i) ;
sum *= 1<<M ;
sum is your answer.

I want to improve speed of my algorithm with multiple rows input. Python. Find average of consequitive elements in list

I need to find average of consecutive elements from list.
At first I am given lenght of list,
then list with numbers,
then am given how many test i need to perform(several rows with inputs),
then I am given several inputs to perform tests(and need to print as many rows with results)
every row for test consist of start and end element in list.
My algorithm:
nu = int(input()) # At first I am given lenght of list
numbers = input().split() # then list with numbers
num = input() # number of rows with inputs
k =[float(i) for i in numbers] # given that numbers in list are of float type
i= 0
while i < int(num):
a,b = input().split() # start and end element in list
i += 1
print(round(sum(k[int(a):(int(b)+1)])/(-int(a)+int(b)+1),6)) # round up to 6 decimals
But it's not fast enough.I was told it;s better to get rid of "while" but I don't know how. Appreciate any help.
Example:
Input:
8 - len(list)
79.02 36.68 79.83 76.00 95.48 48.84 49.95 91.91 - list
10 - number of test
0 0 - a1,b1
0 1
0 2
0 3
0 4
0 5
0 6
0 7
1 7
2 7
Output:
79.020000
57.850000
65.176667
67.882500
73.402000
69.308333
66.542857
69.713750
68.384286
73.668333
i= 0
while i < int(num):
a,b = input().split() # start and end element in list
i += 1
Replace your while-loop with a for loop. Also you could get rid of multiple int calls in the print statement:
for _ in range(int(num)):
a, b = [int(j) for j in input().split()]
You didn't spell out the constraints, but I am guessing that the ranges to be averaged could be quite large. Computing sum(k[int(a):(int(b)+1)]) may take a while.
However, if you precompute partial sums of the input list, each query can be answered in a constant time (sum of numbers in the range is a difference of corresponding partial sums).

How to sum all values for numbers in rows in a file

I am creating a program which involves summing all numbers in the rows of a .txt file. The file is randomly generated, and consists of 100 random numbers, each occupying their own row. What I would like to do is create a list of all the numbers in the rows and then take the sum of that list. For some reason, the code I have is not working:
import os
import math
filename = input("Enter file name: ")
if not os.path.exists(filename):
print(filename, "does not exist")
else:
fin = open(filename)
counter = -1
comments = 0
total = 0
for line in fin:
if "#" in line:
comments += 1
counter = counter + 1
newlist = [[]]
fin = open(filename)
for line in fin:
newlist = [[]]
val = []
for value in line:
val.append(value)
newList.append(val)
total = sum(newList)
print("Count:", counter)
print("Comments:", comments)
print("Total:", total)
The top part runs fine, but the function for the "total" value (the sum which I am trying to derive) is not working. Any advice would be greatly appreciated. Thanks!
If you have a line of numbers separated by whitespace (as opposed to digits not separated), your dual for loop is not going to work as you think:
>>> line = '12 34 56 78 90'
>>> for value in line: print(value)
...
1
2
3
4
5
6
7
8
9
0
As per above, iterating over a string (such as the line you input) will process it one character at a time.
To process whitespace-separated words in a string, you can use something like:
>>> for value in line.split(): print(int(value))
...
12
34
56
78
90
You'll also want to turn them into integers (as shown) if you want to sum them.

How to access the elements of a matrix without lists in this example?

I am quite new at haskell. Our task is to write an algorithm that calculates the dimension of a square matrix. But we are not supposed to use lists. But I dont know how to access to each element of the matrix, which is given like that:
matrixA 1 1 = 0
matrixA 1 2 = 42
matrixA 1 3 = 1337
matrixA 2 1 = 501
matrixA 2 2 = 314
matrixA 2 3 = 301
matrixA 3 1 = 13
matrixA 3 2 = 161
matrixA 3 3 = 271
matrixA _ _ = -1
To access the element of the matrix at row i and column j, you can simply use matrixA i j. In fact, matrixA is a function which maps both indexes to the matrix element.
I guess that the last -1 case represents an invalid value, meaning "indexes outside the matrix". So, it seems that to find the size you just have to query the matrix with larger and larger row/column indexes until you get -1. A basic way to solve that is to proceed recursively.

Desk Check Binary Search

I am here about a query that I am facing.
I was wondering on how I would go about desk checking the following code.
Data set
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
4 7 19 25 36 37 50 100 101 205 220 271 306 321 456 500 /* Numbers are a bit messed up */
Algorithm
binarySearch
SET found TO FALSE
SET bottom TO zero
SET top TO sizeOfList-1
WHILE ( NOT found AND bottom <= top )
SET middle TO (bottom+top) DIV 2
IF searchValue < list element middle THEN
SET top TO middle-1
ELSE
IF searchValue > list element middle THEN
SET bottom TO middle+1
ELSE
SET position TO middle
SET found TO TRUE
ENDIF
ENDIF
ENDWHILE
IF NOT found THEN
RETURN –1
ELSE
RETURN position
ENDIF
The best way to do this is to first draw up a table, with one column for each variable (found, bottom, top, etc.). Then, "be" the computer, step through your program code one line at a time (probably best to write down each line number that you visit to keep track), taking the conditional branches based on the values in your table. Every time you modify a variable, add a new row to your table with the updated values. Eventually, you should reach a return statement, and then you're done.

Resources