Printing a star pattern in swift using stride function - swift

Right now i am trying to print following star pattern using swift 4.
1
12
123
1234
For this i have taken below approach.
for i in stride(from: 1, to: 5, by: 1) {
// prints from 1 to 4
for j in stride(from: 1, to: i, by: 1) {
print(j, separator: "", terminator: "")
}
print("*\n")
}
But right now this will print below output.
*
1*
12*
123*
Any way to fix this issue ?

Here are some logical pattern in swift 4 ,
0
01
012
0123
01234
for i in 0...4{
for j in stride(from: 4, to: i, by: -1){
print( terminator : " ")
}
for k in 0...i{
print(k,terminator : "")
}
print(" ")
}
54321
5432
543
54
5
for i in stride(from: 0, to: 5, by: 1){
for j in stride(from: 5, to: i, by: -1){
print(j , terminator : "")
}
print(" ")
}
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
for i in stride(from: 5, to: 0, by: -1)
{
for j in stride(from: 5, to: i-1, by: -1){
print(j , terminator : "")
}
print(" ")
}
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
for i in stride(from: 5, to: 0, by: -1)
{
for j in 1...i{
print(j, terminator : "")
}
print(" ")
}
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
var value = 0
for i in 1...5 {
for j in 1...i{
value = value + 1
print(value,terminator : "")
}
print(" ")
}
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
for i in 1...5{
for j in 1...i{
print(i+1 - j , terminator : "")
}
print(" ")
}
for i in 1...5{
for j in stride(from: i, to: 0, by: -1){
print(j, terminator : "")
}
print(" ")
}
1
2 7
3 8 13
4 9 14 19
5 10 15 20 25
var value = 0
for i in 1...5{
for j in 1...i{
if j != 1{
value = value + 5
}
else{
value = i
}
print(value , terminator : " ")
}
print(" ")
}
for i in 1...5{
var temp = i
for j in 0...i{
print(temp , terminator : " ")
temp = temp + 5
}
print(" ")
}
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
var value = 1
for i in 1...5{
for j in 1...i{
print(j,terminator : "")
}
for k in 1..<i{
print(i-k,terminator : "")
}
print(" ")
}
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
for i in stride(from: 5, to: 0, by: -1){
for k in stride(from: 5, to: i, by: -1) {
print(terminator : " ")
}
for j in stride(from: 1, to: i+1, by: 1){
print("*",terminator : " ")
}
print(" ")
}
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
for i in 1...5{
for j in stride(from: 5, to: i, by: -1){
print(i,terminator : "")
}
var temp = 1
for k in 1...i{
print(temp,terminator : "")
temp = temp * (i - k) / (k);
}
print(" ")
}
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
for i in 1...5{
for k in stride(from: 5, to: i, by: -1) {
print(terminator : " ")
}
for j in 1...i{
print(j,terminator : " ")
}
print(" ")
}
for i in stride(from: 5, to: 0, by: -1){
for k in stride(from: 5, to: i-1, by: -1) {
print(terminator : " ")
}
for j in stride(from: 1, to: i, by: 1){
print(j,terminator : " ")
}
print(" ")
}
12345
2345
345
45
5
5
45
345
2345
12345
for i in 1...5{
for j in stride(from: i, to: 6, by: 1){
print(j , terminator : "")
}
print(" ")
}
for i in stride(from: 5, to: 0, by: -1)
{
for j in stride(from: i, to: 6, by: 1){
print(j,terminator : "")
}
print(" ")
}
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
for i in 1...5{
for k in 0...i{
print(terminator : " ")
}
for j in stride(from: i, to: 6, by: 1){
print(j , terminator : " ")
}
print(" ")
}
12345
2345
345
45
5
5
45
345
2345
12345
for i in 1...5{
for k in 1...i{
print(terminator : " ")
}
for j in stride(from: i, to: 6, by: 1){
print(j , terminator : "")
}
print(" ")
}
for i in stride(from: 5, to: 0, by: -1)
{
for k in 1...i{
print(terminator : " ")
}
for j in stride(from: i, to: 6, by: 1){
print(j,terminator : "")
}
print(" ")
}
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5
for i in 1...5{
for k in 0...i{
print(terminator : " ")
}
for j in stride(from: i, to: 6, by: 1){
print(j , terminator : " ")
}
print(" ")
}
for i in stride(from: 6, to: 1, by: -1){
for k in 1...i{
print(terminator : " ")
}
for j in stride(from: i-1, to: 6, by: 1){
print(j , terminator : " ")
}
print(" ")
}
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
for i in 0...4{
for j in 0...i{
if j % 2 == 0{
print(1,terminator : " ")
}
else{
print(0,terminator : " ")
}
}
print(" ")
}
1 0 0 0 0
0 2 0 0 0
0 0 3 0 0
0 0 0 4 0
0 0 0 0 5
for i in 1...5{
for j in 1...5{
if j == i{
print(j,terminator : " ")
}
else{
print(0,terminator : " ")
}
}
print(" ")
}

If you want
1
12
123
1234
for i in 1..<5 { // or for i in stride(from: 1, to: 5, by: 1) {
for j in 1...i { // for j in stride(from: 1, through: i, by: 1) {
print(j, terminator: "")
}
print("")
}
If you want
*
**
***
****
for i in 1..<5 {
for _ in 1...i {
print("*", terminator: "")
}
print("")
}

1) Pyramid
*
* *
* * *
* * * *
* * * * *
LINK :-
https://replit.com/#vishakhaagarwa1/DarkorchidSmoothPayware#main.swift
for i in 1...5
{
for _ in stride(from: 5, to: i, by: -1)
{
print(terminator : " ")
}
for _ in 1...i
{
print("*",terminator : " ")
}
print(" ")
}
2) Right Triangle
*
* *
* * *
* * * *
* * * * *
LINK :-
https://replit.com/#vishakhaagarwa1/StingyOldApplicationstack#main.swift
var i = 1
var j = 1
for i in i...5
{
for _ in j...i
{
print("*",terminator : "")
}
print("")
}
3) Mirrored Right Triangle
*
* *
* * *
* * * *
* * * * *
LINK :-
https://replit.com/#vishakhaagarwa1/CookedParallelRobodoc#main.swift
for i in 1...5
{
for _ in stride (from: 5, to: i, by: -1)
{
print(" ",terminator : "")
}
for _ in 1...i
{
print("*",terminator : "")
}
print("")
}
4) Diamond
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
LINK :-
https://replit.com/#vishakhaagarwa1/WarmEmptyDegrees#main.swift
for i in 1...10
{
if i % 2 != 0
{
for _ in stride(from: 10, to: i, by: -1)
{
print(terminator : " ")
}
for _ in 1...i
{
print("*",terminator : " ")
}
print(" ")
}
}
for i in stride(from: 10, to: 1, by: -1)
{
if i % 2 != 0
{
for _ in stride(from: 10, to: i-1, by: -1)
{
print(terminator : " ")
}
for _ in stride(from: 2, to: i, by: 1)
{
print("*",terminator : " ")
}
print(" ")
}
}
5) Downward Triangle
* * * * *
* * * *
* * *
* *
*
LINK :-
https://replit.com/#vishakhaagarwa1/FearlessIndolentByte#main.swift
for i in 1...5
{
for _ in stride(from: i, to: 6, by: 1)
{
print("*",terminator : "")
}
print("")
}
6) Right Pascal’s Triangle
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
LINK :-
https://replit.com/#vishakhaagarwa1/IndianredNervousPrinter#main.swift
for i in 1...5
{
for _ in 1...i
{
print("*",terminator : "")
}
print("")
}
for i in 1...4
{
for _ in stride (from: 5, to: i, by: -1)
{
print("*",terminator : "")
}
print("")
}
7) Sandglass Pattern
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *
LINK :-
https://replit.com/#vishakhaagarwa1/TangibleDeeppinkWatch#main.swift
for i in 1...5
{
for _ in 1...i
{
print(terminator: " ")
}
for _ in stride(from: i, to: 6, by: 1)
{
print("*",terminator : " ")
}
print("")
}
for i in 1...5
{
for _ in stride(from: 6, to: i, by: -1)
{
print(terminator : " ")
}
for _ in 1...i
{
print("*",terminator : " ")
}
print(" ")
}

for i in 0...5{
for _ in 0...i{
print("*", terminator: "")
}
print() }
for i in stride(from: 5, to: 0, by: -1){
for _ in stride(from: i, to: 0, by: -1){
print("*",terminator: "")
}
print() }
output :
*
**
***
****
*****
******
*****
****
***
**
*

var q : String = ""
repeat
{
print("***** Numerical Patterns *****")
print("1 for Number Right Triangle.. ")
print("2 for Sequential Number Right Triangle.. ")
print("3 for Pascal Number Triangle.. ")
print("Enter Your Choice :: ")
guard let index = Int(readLine()!), index > 0 && index < 4 else
{
print(" !!!! WArning :: Wrong Selection.......")
print("\n")
continue
}
var n: Int
print("Enter N Number For Pattern :: ")
let num = readLine()
if let t2 = Int(num!)
{
n = t2
}
else
{
print("wrong input ! taking default [5]")
n = 5
}
switch index {
case 1 :
for i in 1...n
{
for j in 1...i
{
print(j,terminator: " ")
}
print("")
}
case 2 :
var j = 0
for i in 1...n
{
for _ in 1...i
{
j = j+1
print(j,terminator: " ")
}
print("")
}
case 3 :
for i in 1...n
{
//print("i==",i)
for _ in stride(from: n, to: i, by: -1)
{
//print("k==",k)
print(terminator : " ")
}
var no = 1
for j in 1...i
{
//print("j==",j)
print(no,terminator : " ")
no = no * (i - j) / (j)
}
print(" ")
}
default :
print( "Wrong Input............")
}
print("\n")
print("Do you want to continues [PRESS Y/y] OR CLOSE [PRESS N/n] :: ")
q = (readLine()!)
print("\n")
}while(q == "y" || q == "Y")

func starprint(_ int : Int)
{
for i in 1...int
{
print(String.init(repeating: " ", count: int-i)+String.init(repeating: "*", count: i))
}
}
starprint(6)

for i in stride(from: 1, to: 6, by: 1) {
// prints from 1 to 4
for j in stride(from: 1, to: i, by: 1) {
print(j, separator: "", terminator: "")
}
print("")
}

You can use for loop with below sequencing.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
func printLadderNumber(_ n: Int) {
for i in 1...n {
for j in 1...i {
print(j, terminator: "")
}
print("", terminator: "\n")// Helps for printing new line
}
}
printLadderNumber(4)
You can find various Star Patterns Question here
https://janeshswift.com/ios/dsa/array/how-to-print-patterns-in-swift/

1. Pyramid
1
1 2
1 2 3
1 2 3 4
for i in 1...4{
for _ in 1...5-i{
print(terminator:" ")
}
for j in 1...i{
print(j,terminator:" ")
}
print("")
}
2. Inverted Pyramid
1 2 3 4
1 2 3
1 2
1
for i in 1...4{
for _ in 1...i{
print(terminator:" ")
}
for j in 1...5-i{
print(j,terminator:" ")
}
print("")
}

Related

How to calculate height based on item count in swift

How do I archive this result using a simple mathematical formula.
I have an initial offset = 100, and initial count = 0, that i want to increase the offset based on count value. I tried using the below code but it doesn't function correctly.
Example
When count is 0 to 3, then offset should be 100.
When count is
4 to 6, then offset should be 200.
When count is 7 to 9,
then offset should be 300.
When count is 10 to 12, then offset
should be 400.
Attempt
func getHeight(count: Int) ->CGFloat {
var index = 0
for i in 0..<count{
if(i % 2 != 0){
index += 1
}
}
return CGFloat(100 * index)
//return CGFloat(count + 3 / 9 * 100)
}
Testing
print("0 to 3 = \(self.getHeight(count: 0)), expected = 100")
print("0 to 3 = \(self.getHeight(count: 2)), expected = 100")
print("4 to 6 = \(self.getHeight(count: 4)), expected = 200")
print("7 to 9 = \(self.getHeight(count: 7)), expected = 300")
print("10 to 12 = \(self.getHeight(count: 12)), expected = 400")
Results
0 to 3 = 0.0, expected = 100
0 to 3 = 100.0, expected = 100
4 to 6 = 200.0, expected = 200
7 to 9 = 300.0, expected = 300
10 to 12 = 600.0, expected = 400
Formula with integer division:
let cnt = count != 0 ? count : 1
result = 100 * ((cnt + 2) / 3)

Recursive function with loops inside

I am trying to generalize loops within a recursive function...
The basic double loop is the following function:
func multiLoops(start ix:Int, upTo n:Int) {
for i in ix...n {
for j in i+1 ... n+1 {
print("it is \(i) \(j)")
}
}
}
multiLoops(start: 3, upTo: 6)
With the following result:
it is 3 4
it is 3 5
it is 3 6
it is 3 7
it is 4 5
it is 4 6
it is 4 7
it is 5 6
it is 5 7
it is 6 7
My tentative to do the same with a recursive function is the following:
func multiLoopsRecursive(start ix:Int, upTo n:Int, loopNumber:Int){
var loopNumber = loopNumber
var previous_i = ix
func loop(start ix:Int, upTo n:Int) {
for i in ix + 1...n {
print("it is \(previous_i) \(i)")
if loopNumber > 1 {
loopNumber -= 1
previous_i = i
loop(start: previous_i+1, upTo: n+loopNumber)
}
}
}
loop(start: ix, upTo: n)
}
multiLoopsRecursive(start: 3, upTo: 6, loopNumber: 2)
With the following result ...
it is 3 4
it is 4 6
it is 4 7
it is 4 5
it is 4 6
Not really the same as the basic double loop ...
I am really blocked...
Thank you for your help...
Regards
K
Basic double loop with a print loopNumber added:
for i in ix...n {
print("it is loop 1")
for j in i+1 ... n+1 {
print("it is loop 2")
print("it is \(i) \(j)")
}
}
}
multiLoops(start: 3, upTo: 6)
Result:
it is loop 1
it is loop 2
it is 3 4
it is loop 2
it is 3 5
it is loop 2
it is 3 6
it is loop 2
it is 3 7
it is loop 1
it is loop 2
it is 4 5
it is loop 2
it is 4 6
it is loop 2
it is 4 7
it is loop 1
it is loop 2
it is 5 6
it is loop 2
it is 5 7
it is loop 1
it is loop 2
it is 6 7
Larme, Please find your code with the loopNumber added:
var loopNumber = 1
func multiLoopsRecursive(start: Int, upTo: Int) {
print("it is loop \(loopNumber)")
for i in start+1...upTo+1 {
print("it is \(start) \(i)")
}
let newStart = start + 1
if newStart < upTo+1 {
loopNumber += 1
multiLoopsRecursive(start: newStart, upTo: upTo)
}
}
print("Recursive")
multiLoopsRecursive(start: 3, upTo: 6)
Result:
Recursive
it is loop 1
it is 3 4
it is 3 5
it is 3 6
it is 3 7
it is loop 2
it is 4 5
it is 4 6
it is 4 7
it is loop 3
it is 5 6
it is 5 7
it is loop 4
it is 6 7
Thank you for your support
K
Here we are !
The trick is to know in which loop you are to adapt the different variables. I leave in the code the « print loop down and up » for more clarity, but it can be suppressed of course.
Now it works with loops in loops :-)
func loopInLoop (iStart:Int, iEnd:Int, numberOfLoops : Int) {
print("what loop is it! \(loopIndex)")
var iRank = 0
for i in iStart...(iEnd - numberOfLoops + loopIndex) {
iArray[loopIndex-1] = i
iRank += 1
if loopIndex < numberOfLoops {
print ("loop down")
loopIndex += 1
loopInLoop(iStart: iStart + iRank, iEnd: iEnd, numberOfLoops: numberOfLoops)
} else {print("iArray:\(iArray)")}
}
print ("loop up")
loopIndex -= 1
}
let iStart = 3 // initial starting index of first loop, must be <= iEnd
let iEnd = 7 // ending starting index of first loop
let numberOfLoops = 2 // number of loop to execute, must be <= (iEnd-iStart)
var iArray = [Int](repeating: 0, count: numberOfLoops) // Array of indexes
var loopIndex = 1 // initial index of the loop
loopInLoop(iStart: iStart, iEnd: iEnd, numberOfLoops : numberOfLoops)
Result:
what loop is it! 1
loop down
what loop is it! 2
iArray:[3, 4]
iArray:[3, 5]
iArray:[3, 6]
iArray:[3, 7]
loop up
loop down
what loop is it! 2
iArray:[4, 5]
iArray:[4, 6]
iArray:[4, 7]
loop up
loop down
what loop is it! 2
iArray:[5, 6]
iArray:[5, 7]
loop up
loop down
what loop is it! 2
iArray:[6, 7]
loop up
loop up

Getting expression too complex error .please correct me if i have done thing wrong

let b0 = UInt32(block[block.startIndex + 0 + (0 << 2)]) << 0 | UInt32(block[block.startIndex + 1 + (0 << 2)]) << 8 | UInt32(block[block.startIndex + 2 + (0 << 2)]) << 16
b0 = b0 | UInt32(block[block.startIndex + 3 + (0 << 2)]) << 24
let b1 = UInt32(block[block.startIndex + 0 + (1 << 2)]) << 0 | UInt32(block[block.startIndex + 1 + (1 << 2)]) << 8 | UInt32(block[block.startIndex + 2 + (1 << 2)]) << 16
b1 = b1 | UInt32(block[block.startIndex + 3 + (1 << 2)]) << 24
let b2 = UInt32(block[block.startIndex + 0 + (2 << 2)]) << 0 | UInt32(block[block.startIndex + 1 + (2 << 2)]) << 8 | UInt32(block[block.startIndex + 2 + (2 << 2)]) << 16
b2 = b2 | UInt32(block[block.startIndex + 3 + (2 << 2)]) << 24
let b3 = UInt32(block[block.startIndex + 0 + (3 << 2)]) << 0 | UInt32(block[block.startIndex + 1 + (3 << 2)]) << 8 | UInt32(block[block.startIndex + 2 + (3 << 2)]) << 16
b3 = b3 | UInt32(block[block.startIndex + 3 + (3 << 2)]) << 24
If you just format this code properly, you'll see there's a very clear pattern:
let start = block.startIndex
let b0 = UInt32(block[start + 0 + (0 << 2)]) << 0
| UInt32(block[start + 1 + (0 << 2)]) << 8
| UInt32(block[start + 2 + (0 << 2)]) << 16
| UInt32(block[start + 3 + (0 << 2)]) << 24
let b1 = UInt32(block[start + 0 + (1 << 2)]) << 0
| UInt32(block[start + 1 + (1 << 2)]) << 8
| UInt32(block[start + 2 + (1 << 2)]) << 16
| UInt32(block[start + 3 + (1 << 2)]) << 24
let b2 = UInt32(block[start + 0 + (2 << 2)]) << 0
| UInt32(block[start + 1 + (2 << 2)]) << 8
| UInt32(block[start + 2 + (2 << 2)]) << 16
| UInt32(block[start + 3 + (2 << 2)]) << 24
let b3 = UInt32(block[start + 0 + (3 << 2)]) << 0
| UInt32(block[start + 1 + (3 << 2)]) << 8
| UInt32(block[start + 2 + (3 << 2)]) << 16
| UInt32(block[start + 3 + (3 << 2)]) << 24
Each b constant is just the numbers 0...3 transformed in similar ways, all bitwise-OR'ed together. Sounds like a job for map/reduce:
let start = block.startIndex
let b0 = (0...3).lazy.map{ UInt32(block[start + $0 + (0 << $0)]) << $0 * 8 }.reduce(0, |)
let b1 = (0...3).lazy.map{ UInt32(block[start + $0 + (1 << $0)]) << $0 * 8 }.reduce(0, |)
let b2 = (0...3).lazy.map{ UInt32(block[start + $0 + (2 << $0)]) << $0 * 8 }.reduce(0, |)
let b3 = (0...3).lazy.map{ UInt32(block[start + $0 + (3 << $0)]) << $0 * 8 }.reduce(0, |)
This can be even further simplified, if you made a b array with 4 elements, rather than 4 seperate b# variables:
let start = block.startIndex
let b = (0...3).map{ x -> UInt32 in
fatalError("I don't know what the number x represents, so I just named it x. Give it a better name.")
return (0...3).lazy
.map{ UInt32(block[start + $0 + (x << $0)]) << $0*8 }
.reduce(0, |)
}

Openbugs too many constants error

I'm trying to run the code below in which I used a zero trick for my log-likelihood expression(phi is my log-likelihood):
model{
for (l in 1:k) {
d.1[l] ~ dbern(p.1)
d.2[l] ~ dbern(p.2)
d.3[l] ~ dbern(p.3)
d.4[l] ~ dbern(p.4)
}
for (l in 1:k) {
zeros[l] <- 0
zeros[l] ~ dpois(phi[l])
u.1[l] <- pow((1 - p.1), (1 - d.1[l]))
u.2[l] <- pow((1 - p.2), (1 - d.2[l]))
u.3[l] <- pow((1 - p.3), (1 - d.3[l]))
u.4[l] <- pow((1 - p.4), (1 - d.4[l]))
f.1[l] <- pow(p.1, d.1[l]) * pow((1 - p.1), (1 - d.1[l]))
f.2[l] <- pow(p.2, d.2[l]) * pow((1 - p.2), (1 - d.2[l]))
f.3[l] <- pow(p.3, d.3[l]) * pow((1 - p.3), (1 - d.3[l]))
f.4[l] <- pow(p.4, d.4[l]) * pow((1 - p.4), (1 - d.4[l]))
a[l] <- pow((pow(u.3[l], -theta.1) + pow(u.4[l], -theta.1) -
1), (theta.0/theta.1 - 2)) * (-1) * ((theta.0/theta.1) *
(-1) + pow((theta.0/theta.1), 2))
b[l] <- pow((pow(u.1[l], -theta.2) + pow(u.2[l], -theta.2) -
1), (theta.0/theta.2 - 2)) * (-1) * ((theta.0/theta.2) *
(-1) + pow((theta.0/theta.2), 2))
c[l] <- pow(pow((pow(u.3[l], -theta.1) + pow(u.4[l],
-theta.1) - 1), -(1/theta.1)), -theta.0) + pow(pow((pow(u.2[l],
-theta.2) + pow(u.1[l], -theta.2) - 1), -(1/theta.2)),
-theta.0) - 1
d[l] <- pow((pow(u.3[l], -theta.1) + pow(u.4[l], -theta.1) -
1), (theta.0/theta.1 * 2 - 2)) * pow(theta.0/theta.1,
2)
e[l] <- pow((pow(u.1[l], -theta.2) + pow(u.2[l], -theta.2) -
1), (theta.0/theta.2 * 2 - 2)) * pow(theta.0/theta.2,
2)
phi[l] <- log((a[l] * b[l]) * (1 + theta.0)/pow(c[l],
-(2 + 1/theta.0)) + (d[l] * b[l] + a[l] * e[l]) *
((1 + theta.0) * (1 + 2 * theta.0))/pow(theta.0,
3) * pow(c[l], -(3 + 1/theta.0)) + (d[l] * e[l]) *
((1 + theta.0) * (1 + 2 * theta.0) * (1 + 3 * theta.0))/pow(theta.0,
4) * pow(c[l], -(4 + 1/theta.0))) + log(pow(theta.1,
2) * pow((u.3[l] * u.4[l]), -(1 + theta.1)) * pow(theta.2,
2) * pow((u.1[l] * u.2[l]), -(1 + theta.2)))
}
p.1 ~ dunif(0 , 1)
p.2 ~ dunif(0 , 1)
p.3 ~ dunif(0 , 1)
p.4 ~ dunif(0 , 1)
theta.0 ~ dunif(0 , 2)
theta.1 ~ dunif(0 , 2)
theta.2 ~ dunif(0 , 2)
}
with data:
k <- 16
d.1 <- c(0 , 0, 0 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 1 , 0)
d.2 <- c(0 , 1, 0 , 0 , 0 , 0 , 1 , 0 , 1 , 1 , 0 , 1 , 0 , 0 , 1 , 0)
d.3 <- c(1 , 0, 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0)
d.4 <- c(0 , 1, 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0)
The model is syntactically correct and the data is loaded but I got the following error:
logical expression contains too many constants
Can anyone please help me solve that problem?
BUGS does not like expressions with too many constants. You need to introduce a sub-step to break them up. See point (c) under 'Some Error Messages' in this page:
http://mathstat.helsinki.fi/openbugs/Manuals/TipsTroubleshooting.html

Understanding recursive function with implicit return statement

Below recursive method sums the integer values between a range
def sumInts(a: Int, b: Int): Int = {
if(a > b) 0
else {
println(a +"," + b)
a + sumInts(a + 1 , b)
}
}
So sumInts(2 , 5) returns 14
I'm confused about how the recursive call to sumInts sums the integer range. Can explain textually how this method works ?
How does sumInts return the incremented value ?? Perhaps I am missing something fundamental to recursion here
It calculates the sum of values in the range [a, b] by first calculating the sum of the range [a+1, b] (by recursively calling sumInts(a + 1 , b)) then adding a to it.
[Update] In Scala, the return statement is optional; functions return the value of the last expression evaluated. Thus the above function body is equivalent to
if(a > b) return 0
else {
println(a +"," + b)
return a + sumInts(a + 1 , b)
}
[/Update]
Which for the range [2, 5] it would do the following (I removed the println call for the sake of simplicity, and added brackets to mark recursive calls):
if(2 > 5) 0 else 2 + sumInts(2 + 1, 5) which, the condition being false, evaluates to
2 + sumInts(3, 5)
2 + (if(3 > 5) 0 else 3 + sumInts(3 + 1, 5)) which evaluates to
2 + (3 + sumInts(4, 5))
2 + (3 + (if(4 > 5) 0 else 4 + sumInts(4 + 1, 5))) which evaluates to
2 + (3 + (4 + sumInts(5, 5)))
2 + (3 + (4 + (if(5 > 5) 0 else 5 + sumInts(5 + 1, 5)))) which evaluates to
2 + (3 + (4 + (5 + sumInts(6, 5))))
2 + (3 + (4 + (5 + (if(6 > 5) 0 else 6 + sumInts(6 + 1, 5))))) which, the condition being true, evaluates to
2 + (3 + (4 + (5 + (0))))